Shift to Sprint that changes

I’m trying to make a sprint to shift script that can change over time, this is the script that I have that doesn’t work the way I want it to.

local norm_spd = 10 
local ke_y = Enum.KeyCode.LeftShift 



local fovMax = { FieldOfView = 70 + (speed/5) }
local fovMin = { FieldOfView = 70 }
local thing = game.Workspace.CurrentCamera
local tween = game.TweenService:Create(thing, TweenInfo.new(0.4, Enum.EasingStyle.Sine), fovMax)
local tween2 = game.TweenService:Create(thing, TweenInfo.new(0.4, Enum.EasingStyle.Sine), fovMin)
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, processed)
   if processed then return end
   if input.UserInputType == Enum.UserInputType.Keyboard then
   		if input.KeyCode == ke_y then
       		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed
			tween:Play()
			-- tween starts 1 and stops 2
   		end
	end
end)

UIS.InputEnded:Connect(function(input) 
	if input.KeyCode == ke_y then
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = norm_spd
		tween2:Play()
		-- tween starts 2 and stops 1
	end
end)

I made it so that the sprint speed is under a numbervalue under ReplicatedStorage, but when the number value changes, it only follows the initial number when you start the game. I’m not sure how to fix this as I’m a beginner scripter.

1 Like

You declared your speed variable with NumberValue.Value, this means that you are only indexing (reading) the NumberValue’s value once.

You can connect a .Changed event to the NumberValue and make it update your speed variable to NumberValue.Value.
Or alternatively replace:
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed
with:
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = NumberValue.Value

1 Like

It works perfectly now, thank you!

You’re welcome. I’m happy to have helped you fix your script. :smile: