Changing Speed By TextBox

Hello. So I am making a game for my friend, and I have a specific job for the GUI. I have to make a Textbox that can change the player’s speed. My script does print everything that needs to be printed, but that does not change the player’s speed.

local player = game.Players.LocalPlayer
local speed = player.PlayerGui.Menu.Settings.speed

local function onFocusLost(enterPressed, _inputobject)
	if enterPressed then
		local char = player.Character
		local hum = char:FindFirstChild("Humanoid")
		local Walkspeed = hum.WalkSpeed
		print("Got locals")
		Walkspeed = script.Parent.Text
		print("set speed to..."..Walkspeed)
	end
end

script.Parent.FocusLost:Connect(onFocusLost)

The issue is because you are storing the WalkSpeed in the variable, then changing the variable. You have to change the WalkSpeed directly, because the way you’re doing it, it’s only setting the variable, like I just said.


I would also change their speed on the server with a RemoteEvent instead of on the client so you can better limit the WalkSpeed change if you want to (you really should).

Add a RemoteEvent named “PlayerSpeed” into ReplicatedStorage, then replace your client code with this:

local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer

local textbox = script.Parent
local speed = player.PlayerGui.Menu.Settings.speed

local function onFocusLost(enterPressed, _inputobject)
	if enterPressed then
replicatedStorage.PlayerSpeed:FireServer(textbox.Text)
	end
end

textbox.FocusLost:Connect(onFocusLost)

In a server script in ServerScriptService, use this code:

local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local minSpeed = 5
local maxSpeed = 150

local function ChangePlayerSpeed(player: Player, speed: number)
    local character = player.Character
    local humanoid = character:WaitForChild("Humanoid")

    if character and humanoid then
        if speed >= minSpeed and speed <= maxSpeed then
            humanoid.WalkSpeed = speed
        end
    end
end

replicatedStorage:WaitForChild("PlayerSpeed").OnServerEvent:Connect(ChangePlayerSpeed)

If you want to remove the limitations on the speed, then you can remove the minSpeed and maxSpeed variables, as well as the if statement checking the speed. Or you could change the minSpeed variable to zero and maxSpeed variable to math.huge

I also apologize for any poor formatting. I’m on my phone typing this out.

doing this justmeans that Walkspeed is the current speed on the humanoid. do

hum.WalkSpeed = script.Parent.Text

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