I'm having trouble with this value-based sprinting script

The goal is for my character’s WalkSpeed to a value named BoostingSpeed if another valued named Boosting is true. BoostingSpeed is just the character’s WalkSpeed multiplied by 2.

I already have a remote event set up to where if the shift key or the right joystick on a gamepad is pressed, Boosting will be set to true. This part already works but this is the part I’m stuck on. This is a server script inside of StarterCharacterScripts btw.

local Boosting = script.Parent.CharacterValues.Actions:WaitForChild("Boosting")

while true do
	wait()
	if Boosting.Value == true then
		script.Parent.Humanoid.WalkSpeed = Boosting.BoostingSpeed.Value
	else
		script.Parent.Humanoid.Walkspeed = Boosting.NormalSpeed.Value
	end
end

This is what happens.
https://gyazo.com/822cc1023d64499e0c850bbea60c5492

No errors even show up in the Output, which makes this really confusing. Boosting is getting set to true but no WalkSpeed changes were made. I tried making this a local script and moving it to different places like StarterPlayerScripts, making it activate a RemoteEvent, putting it in ServerScriptService, nothing works and it’s starting to stress me. Can someone point out what I’m doing wrong or correct me?

How are you changing the Boosting.Value? If its with a local script, the server won’t detect the change, so you might have to fire a remote event that changes the value on the server as well.

I said this in the 2nd paragraph. I already have a different script in StarterGUI that fires a remote event when the shift key is held down. This event sets Boosting.Value to true.

So you have a local script firing a remote event to the server, and a server script with an OnServerEvent function that changes the value?

In this line you have a lowercased WalkSpeed

script.Parent.Humanoid.Walkspeed = Boosting.NormalSpeed.Value

Thanks for pointing that out! The script still doesn’t work though :frowning:

That’s right. Every time I hold down the shift key an OnServerEvent sets Boosting to true.

Try to change the WalkSpeed with hardcoded values first to see if that works.

For example like this:

local Boosting = script.Parent.CharacterValues.Actions:WaitForChild("Boosting")
while true do
	wait()
	if Boosting.Value == true then
		script.Parent.Humanoid.WalkSpeed = 32
	else
		script.Parent.Humanoid.Walkspeed = 16
	end
end

If it works then the problem is somewhere in the Boosting values that you have.

I could have done that if I wanted too, but I want things to effect if a player can Boost or not. Even though what I’m about to say doesn’t have to do with the problem, I want things to be able to effect a player’s boosting ability (which is just another word for sprint).
For example,
cut
There’s a value called CanBoost. The Boosting value can only be set to true if CanBoost is true. CanBoost will be set to false if a player takes a very bad hit, if it’s stunned, or if it’s frozen or stuff along the lines of that. BoostingSpeed contains the number of the player’s WalkSpeed, but it’s always multiplied by 2. That’s the WalkSpeed I want the player to be at when the shift key is being held. NormalSpeed is the number of the player’s normal WalkSpeed. When Boosting == false then the player’s WalkSpeed will revert to that number. If the player drinks a speed potion, NormalSpeed will be set to a higher number and BoostingSpeed will be that number times 2. I didn’t bother with the agility values (JumpHeight) because I’m trying to get the speed down first. You get what I mean here?

So I actually recreated this myself and it works for me. But the thing is, that I don’t think you need the remote event to check if a player is holding shift key down.

The remote event might be useful when something external affects the player but for movement, it can easily be done like this:

local CharacterValues = script.Parent:WaitForChild("CharacterValues")
local Actions = CharacterValues:WaitForChild("Actions")
local Boosting = Actions:WaitForChild("Boosting")

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Boosting.Value = true
	end
end)

uis.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Boosting.Value = false
	end
end)

while true do
	wait()
	if Boosting.Value == true then
		script.Parent.Humanoid.WalkSpeed = Boosting.BoostingSpeed.Value
	else
		script.Parent.Humanoid.WalkSpeed = Boosting.NormalSpeed.Value
	end
end

Also I wouldn’t use while wait() loop because it’s just inefficient. When you do wait() alone it actually waits for around 2 frames.

I would recommend doing it like this:

local CharacterValues = script.Parent:WaitForChild("CharacterValues")
local Actions = CharacterValues:WaitForChild("Actions")
local Boosting = Actions:WaitForChild("Boosting")

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Boosting.Value = true
	end
end)

uis.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Boosting.Value = false
	end
end)

Boosting:GetPropertyChangedSignal("Value"):Connect(function()
	if Boosting.Value == true then
		script.Parent.Humanoid.WalkSpeed = Boosting.BoostingSpeed.Value
	else
		script.Parent.Humanoid.WalkSpeed = Boosting.NormalSpeed.Value
	end
end)

Or use a RunService which runs every frame and is more efficient that while wait().