Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Walkspeed wont change when player clicks button?

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)

1 answer

✓ Accepted answer

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)

1 comment

0

Thank you! I learned a ton from this :>

This discussion is preserved as it was. New answers, comments, and votes are closed.