Sprint button - need some help

Hello!

Small script to use a bolean value to change the player’s speed
open a frame and change the colour of an imagelabel. It. It does change the speed, only initially but will not funciton past that.

local run = script.Parent.runnin

script.Parent.MouseButton1Click:Connect(function()
	if run.Value == false then
		script.Parent.Parent.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 40
		script.Parent.Selected.ImageColor3 = Color3.new(255, 62, 62)
		run.Value = true
	else 			
		script.Parent.Parent.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 18
		script.Parent.Selected.ImageColor3 = Color3.new(61, 104, 122)
		run.Value = false
		end
	
end)
1 Like

You should probably set the run.Value to false in the beginning of the script, outside the function.

Where is the script located, and is it a Server or Local script?

1 Like

local script, I did add run.Value = false just before the if-then function.
It stops working after the line to change the speed to 40.

2 Likes

What seems to be the problem with the script? I have done many sprint script, so I could help out,
Sorry worded that wrong do you get any errors?

1 Like

Ah, well is does chAnge player speed to 40, but the rest of the script simply stops functioning. The color doens’t change, the value isnt set to true

2 Likes

Try changing the value first, then everything else. Also do you get any errors?

Also this is in a local script right?

2 Likes

Yep, it’s local.

welp too short a sentence

1 Like

Im asumming that you don’t get any errors No?

Wait i got one this time-

Players.kitkatsncoffee.PlayerGui.MainScreen.Buttons.Run.LocalScript:6: attempt to index boolean with 'ImageColor3

Can I see all of the items you have in the explorer/You’re Gui?
Because it seems like there might be a problem in how you locate the items

You’re way of locating the players character is not that well built and can cause errors.
Try using

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

to locate the player’s character

1 Like

1 Like

Hope you don’t mind my pause, but im scripting to find out what you’re problem is, on you’re script

1 Like

I think I have found you’re problem.
You see you have an image that is call “Selected” Well… that is also a boolen inside of the object called run. so you’re script thinks you’re trying to change the color of an boolen and not an object.

Just change the name of the object call “Selected” to something else and make sure you change it in the script too. also do what i said about locating the players character
do some like this to change the players walkspeed

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local SprintValue = script.Parent.runnin

script.Parent.MouseButton1Click:Connect(function()
	if SprintValue.Value == false then
		SprintValue.Value = true
		
		Character:FindFirstChild("Humanoid").WalkSpeed = 40
		script.Parent.Selected.ImageColor3 = Color3.new(255, 62, 62)
		
	else
		SprintValue.Value = false
		
		Character:FindFirstChild("Humanoid").WalkSpeed = 16
		script.Parent.Selected.ImageColor3 = Color3.new(61, 104, 122)--- See here the word selected need to be changed, change the name of that object 
	end
end)

image: is bad be cause it looks like this to the script
image

Change it to something like this
image
and in the script change these lines script.Parent.Selected.ImageColor3 to this script.Parent.SprintSelectedTab.ImageColor3

2 Likes

That makes sense! Thanks a heap. She’s all good now.

Sorry about the late reply, busy here, but i appreciate you taking some time to help me out there.

1 Like

No problem, put me as solution if you’d like. So everyone knows this problem is solved

1 Like
local player = script:FindFirstAncestorOfClass("Player")
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local button = script.Parent
local selected = button:WaitForChild("Selected")
local run = button:WaitForChild("runnin")

button.MouseButton1Click:Connect(function()
	run.Value = not run.Value
	humanoid.WalkSpeed = if run.Value then 40 else 18
	selected.ImageColor3 = if run.Value then Color3.new(1, 0.3, 0.3) else Color3.new(0.3, 0.5, 0.5)
end)