Sprint button isn't working

hi, My sprint button is properly working

heres the script
local player = game.Players.LocalPlayer
local Sprint = false

script.Parent.MouseButton1Click:Connect(function()
if Sprint == true then
Sprint = false
player.Character:WaitForChild(“Humanoid”).WalkSpeed = 16
script.Parent.Parent.TextLabel.Text = “off”
else
Sprint = true
player.Character:WaitForChild(“Humanoid”).WalkSpeed = 26
script.Parent.Parent.TextLabel.Text = “on”
end
end)

I was going to screen record but I couldn’t screen record the whole screen with the properties in it. So what happen is that when the player press the button it will increase the player walkspeed, it does that but as soon as the player move, the player walkspeed reset to 16.

1 Like

Please try to format code with three backticks at top and end of code :slight_smile:

local player = game.Players.LocalPlayer
local Sprint = false

script.Parent.MouseButton1Click:Connect(function()
 if Sprint == true then
   Sprint = false
   player.Character:WaitForChild(“Humanoid”).WalkSpeed = 16
    script.Parent.Parent.TextLabel.Text = “off”
 else
    Sprint = true
    player.Character:WaitForChild(“Humanoid”).WalkSpeed = 26
    script.Parent.Parent.TextLabel.Text = “on”
 end
end)

Doesn’t really seem like anything is wrong, but instead I have doubt that the script.Parent is a Text/Image button, can you make sure?

1 Like

the parent is for sure and soz about the code format i forgot to format it lol

1 Like

Are you using it in a localscript? Or did you use a script. Because script aren’t supposed to run in StarterGui/PlayerGui

1 Like

its in startergui/playergui and its a localscript but the thing that isn’t working is that it works but as soon as you move the walkspeed reset to 16

I wish I could screen record the properties.

Can you try putting a print inbetween both? Let’s check if it only fires once.

thanks for the reply, inbetween where exactly?, i’ve try putting prints in the if and else statement and they work perfectly well and they print onced.

Do you have any errors in your script?
Do you have any errors in the output after clicking the button?

Are you sure that these quotes (“Humanoid”) don’t cause any problems?

no there was no error in the output at all and the humanoid quote work but for some reason the walkspeed reset to 16 after moving

Is this a local script or a server script?

local script in the imagebutton

Maybe you have other scripts somewhere and they reset the walkspeed?
Check that.

Also, change your code to this:

local player = game.Players.LocalPlayer
local Sprint = false

script.Parent.MouseButton1Click:Connect(function()
	print("click");
	if Sprint then
		print("if");
		Sprint = false
		player.Character:WaitForChild("Humanoid").WalkSpeed = 16
		script.Parent.Parent.TextLabel.Text = "off"
	else
		print("else");
		Sprint = true
		player.Character:WaitForChild("Humanoid").WalkSpeed = 26
		script.Parent.Parent.TextLabel.Text = "on"
	end
end)

And tell me what does it print.

k I will try, thank you :smiley:

actually good point about checking any other script that reset it because I have another script for PC and if the player isn’t pressing shift then the walkspeed is 16, thanks for reminding me that, thank you.