ROBLOX 获取点击砖块的玩家的名字

ROBLOX Get name of a player that clicked a brick

我有这个脚本:

local giver = 1

function onClicked()
    game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value = game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value + giver
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

现在我需要以某种方式获取点击它的玩家的名字并将其放在我需要的地方。

ClickDetectors's MouseClick事件有"Clicking Player"作为参数,所以你可以这样做:

local giver = 1

function onClicked(Player)
    Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + giver
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

但是,这需要将 FilteringEnabled 设置为 false(不推荐)。

要解决此问题,请使用以下代码在砖块中制作 LocalScript

script.Parent.ClickDetector.MouseClick:connect(function(Player)
    game.ReplicatedStorage:WaitForChild("BrickClick"):InvokeServer(script.Parent)
end)

并在 Script placed in the ServerScriptService 中输入:

local Listener = game.ReplicatedStorage:FindFirstChild("BrickClick")
if Listener == nil then
    Listener = Instance.new("RemoteFunction")
    Listener.Name = "BrickClick"
    Listener.Parent = game.ReplicatedStorage
end

function Listener.OnServerInvoke(Player,Brick)
    Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + 1
end

我不会将您指向 wiki page 以供进一步阅读,即使它包含了您需要的一些信息,但它包含的信息太少了。

ClickDetector's MouseClick info, the guide about FilteringEnabled and the guide about RemoteFunctions更好。

试试这个!

script.Parent.MouseClick:Connect(function(Player)
-- Kill The Player
-- The parameter is referring to game.Players So if you want to do a kill button use .Character
Player.Character:BreakJoints()

-- Change The Color To Red (Other details)
    script.Parent.Parent.BrickColor = BrickColor.new("Really red")
    script.Parent.MaxActivationDistance = 0

-- Wait 4 Secs
wait(5)

-- Change The Color To Green
script.Parent.Parent.BrickColor = BrickColor.new("Lime green")
script.Parent.MaxActivationDistance = 50
end)