If this is your entire script, then the first problem is that the function SetSpeed
is only being called once, when the server loads. The player would have no time to input a value so this won’t work. You need to first add some listener event for detecting when the player gives an input. For example, you can use TextBox | Roblox Creator Documentation to listen for when a player finishes typing an input. This is client sided, so no server script required here!
TypeFrame.TextBox.FocusLost:Connect(function() -- listen for player's input
-- do something
end)
Second, you’re greatly limiting yourself by checking for a specific value of 100
. What if the player wants to have the walkspeed of 99? You would need an if statement for every single possible value, which is unrealistic. You can instead convert the player’s input to a number and set the walkspeed to that value.
See: Converting Strings
TypeFrame.TextBox.FocusLost:Connect(function()
local speed = tonumber(TypeFrame.TextBox.Text) -- convert to number
if speed then -- verifies that the player input a number!
-- change speed
end
end)
Now, you may notice that there is no Player variable being passed around, and that’s because you can always grab the player variable out of thin air from the client with game.Players.LocalPlayer
! I like to grab the player (and any other parts of the player that are needed) at the top of the script just to make sure everything is loaded before doing anything.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
Now that we are sure that the player’s Humanoid is loaded, we can update the player’s walkspeed without problem! Walkspeed is also automatically replicated across servers, which means you don’t need to do anything with the server. With that, our final script will look like this:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local TypeFrame = script.Parent.TypeFrame -- or wherever your frame is!
TypeFrame.TextBox.FocusLost:Connect(function()
local speed = tonumber(TypeFrame.TextBox.Text)
if speed then -- verifies that the player input a number!
Humanoid.WalkSpeed = speed
print(Player.Name .. " has set their speed to " .. speed .. "!")
end
end)
(I would remove that print statement, but it’s always good to know that your script works as you’re developing!)