So I'm scripting a button that changes the players walkspeed to 0 when they click it. This is all supposed to happen to only the player that clicked the button. Problem happens is that.. Nothing happens when a person clicks the button. Here's the script that's inside a local script.
local player = game.Players:FindFirstChild() script.Parent.ClickDetector.MouseClick:Connect(function() if player then player.Character.Humanoid.WalkSpeed = 0 else repeat until player end end)
Firstly, a LocalScript
is only operable on a Client-end. The ClickDetector Instance is only usable in a physical environment, I.e workspace
, which belongs to the Server. Therefore, a ServerScript
should be used in this situation; this doesn't mean that a LocalScript
can't listen for the MouseClick signal from a localized environment, but it's not recommended, nor is it really practical.
Secondly, the FindFirstChild(Name: string)
function is used incorrectly and will error. Regardless, you cannot reference a local Player Object from the Server, the Client and the Server are two very different entities.
Fortunately, our MouseClick
signal does provide the Client that interacted with it, so we can simply use that:
local ClickDetector = script.Parent local function OnClick(Player) local Character = Player.Character --------------- if (Character) then --------------- Character.Humanoid.WalkSpeed = 0 end end ClickDetector.MouseClick:Connect(OnClick)