TextBox, setting Speed

Hello to those who are reading this post of many, I’ve been looking at the “Text Box” Frame of which you can type inside of the frame and I wanted to try to set a Player’s speed using this as an example:

Player types “100” and their speed gets set to 100.

I’ve tried using a Remote Event of something like:


-- Client

function SetSpeed(Player, Status)
    if TypeFrame.TextBox.Text == "100" then
        player.Character:WaitForChild("Humanoid").WalkSpeed = player.Character:WaitForChild("Humanoid").WalkSpeed + 100
        print(player.Name .. " Has set their speed to 100! ")
    end
end

Speed.OnClientEvent:Connect(SetSpeed)

-- Server

Speed:FireServer()

I’m still in a learning stage of knowing how to use RemoteEvents in different ways but this doesn’t seem to work. If you have any ideas as of how I could do something like this or a fix to it, please let me know!

1 Like

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!)

4 Likes

Thanks so much! I just read the entire thing and I have a better understanding now, thanks a lot!

2 Likes

You really helped me! Thanks a lot

2 Likes