GUI Button change player's speed?

I am attempting to make it so players cannot walk when in the game menu (or until they press the play GUI Button)

So basically I am creating a GUI Menu that shows up when players join the game. My only problem is that I want to keep players WalkSpeed at 0 until they press the Play button, but I honestly have no idea how to find the humanoid of the player who clicked the GUI button.
At the moment I’m using a MouseButton1Click Function for the Play Button GUI. I also have it so that the GUI starts in ReplicatedFirst and a LocalScript automatically the menu to the PlayerGui. (obviously)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried looking around and couldn’t find anything similar to a topic like this.

Side note - I’m not asking anyone to write any code for me, I just need assistance in finding the player who clicked the button and changing their walk speed, thanks.
Also, I’m not sure if I posted this in the correct category, apologies if I didn’t.

1 Like

In this case it’s best to have a RemoteEvent fired that would basically control and manipulate the player’s Walkspeed. Calling on the remote event could be something as simple as:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, spd) -- (plr is simply the player that fired the event)
if plr.Character and plr.Character:FindFirstChild("Humanoid") then
plr.Character.Humanoid.WalkSpeed = spd
end
end)

Then in a local script stored within the buttons whenever it detects a mouse click, just have it fire server such as:

game.ReplicatedStorage.RemoteEvent:FireServer(16)

Hello, thank’s for your response!

I attempted putting this code in a LocalScript that functions when a person clicks the TextButton, but it doesn’t seem to be working.
(RemoteEvent in ReplicatedStorage is named LoadInWalkSpeed)

script.Parent.MouseButton1Click:Connect(function()

game.ReplicatedStorage.LoadInWalkSpeed.OnClientEvent:Connect(function(plr, spd) -- (plr is simply the player that fired the event)
	if plr.Character and plr.Character:FindFirstChild("Humanoid") then
		plr.Character.Humanoid.WalkSpeed = 100
	end
end
end)
game.ReplicatedStorage.LoadInWalkSpeed:FireServer(16)

Apologies if I’m having a small skill issue, I’m not very good with RemoteEvents yet.

This is supposed to be placed in a regular script (Most preferably in ServerScriptService). It will act as basically the server side to the event to where it will handle manipulating the players walk speed.

This should also be OnServerEvent as you are awaiting a response from the Server.

-- Script in ServerScriptService
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr: Player, spd: IntValue)
	if plr.Character and plr.Character:FindFirstChild("Humanoid") then -- Condition to check if the player's character exists and it contains a Humanoid
		plr.Character.Humanoid.WalkSpeed = spd -- Sets the player's walk speed to whatever is sent from the local script (spd value)
	end
end)

-- LocalScript
script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RemoteEvent:FireServer(16) -- 16 here is simply a placeholder to what speed the player should be set to.
end)

This would simply be a general idea of how it will behave. Added some comments so you could possibly understand how it manages to pass the data around. Just note that this is just a general basic idea as of course there is also security protocols and optimization to ensure that the remote event isn’t exploited on by other players. This just gives you a basic logic of how it should work. (e.g. steps to adding more security to the code could be things like not passing the speed value via the remote event rather having a key that basically tells the code how it could behave or having sanity checks in the server script).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.