Toggle sprint button not working?

Hiya guys! I’m working on a sprinting button for mobile. The problem is that it won’t change the WalkSpeed. Is this because it’s in a LocalScript or what? Can you change the WalkSpeed from a LocalScript?

local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local WalkSpeed = Character:WaitForChild("Humanoid").WalkSpeed

local isTrue = false

script.Parent.Activated:Connect(function()
	print(isTrue)
	if isTrue == false then
		isTrue = true
		WalkSpeed=30
		print(WalkSpeed)
	else if isTrue == true then
			isTrue=false
			print(WalkSpeed)
			WalkSpeed=16
		end
	end
end)
1 Like

Since you’re using a local script, it’d only update the WalkSpeed to that player only. Meaning the server won’t be able to see those changes.

I tested it out and here are the results:

false
30

true
30

And so on.

Try this:

local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local WalkSpeed = Character:WaitForChild("Humanoid").WalkSpeed

local isTrue = false

script.Parent.Activated:Connect(function()
	print(isTrue)
	if not isTrue then
		isTrue = true
		WalkSpeed=30
		print(WalkSpeed)
	else 
		isTrue=false
		WalkSpeed=16
		print(WalkSpeed)
	end
end)

This should now return you:

false
30

true
16

Note:
If you really need to change the WalkSpeed of the Player so that everyone in the game should see that change, then use a remote event.

1 Like

While the number itself wont change on the server if it is done on a local script, the actual speed will change, as the player has the network ownership of his own character.

Your issue is this

Now in the WalkSpeed variable you have not the walkspeed property of the humanoid, but the value of the walkspeed, so just a number. Change your script with this.

local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local hum = Character:WaitForChild("Humanoid")

local isTrue = false

script.Parent.Activated:Connect(function()
	print(isTrue)
	if isTrue == false then
		isTrue = true
		hum.WalkSpeed=30
		print(WalkSpeed)
	else if isTrue == true then
			isTrue=false
			print(WalkSpeed)
			hum.WalkSpeed=16
		end
	end
end)

It can stay a local script, it will work, or you can change it to server script, which will also work.

P.S. Only if you change it to server script, you need to reference player variable otherwise, as game.Players.LocalPlayer works only in local scripts. So better just leave it as a local script

I forked this from one of my creations.

local UserInputService = game:GetService("UserInputService")
local RunButton = Frame:WaitForChild("RunButton") -- Define your own RunButton

local function ToggleRunning()
	if Humanoid.WalkSpeed == 18 then
		Humanoid.WalkSpeed = 30
	else
		Humanoid.WalkSpeed = 18
	end
end

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if GameProcessed then return end
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		ToggleRunning()
	end
end)
local Debounce = false
RunButton.Activated:Connect(function()
	if not Debounce then
		Debounce = true
		ToggleRunning()
		task.wait(0.2)
		Debounce = false
	end
end)
1 Like