Sprint script does not work

Hey so I was trying to make this sprint script and well, this video kinda shows my problem


if u stop moving it still plays the animation.

i’ve been trying to fix this and nothing seems to work, if i could get any help here i would appreciate it a lot.

script:

local Running_Walkspeed = 20
local playersService = game:GetService("Players")
local Key = Enum.KeyCode.LeftShift
local Camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local TweenInformation = TweenInfo.new(1) 
local Tween = game:GetService("TweenService"):Create(Camera,TweenInformation,{FieldOfView=95})
local Tween2 = game:GetService("TweenService"):Create(Camera,TweenInformation,{FieldOfView=70})
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animation = script:WaitForChild("Anim")
local sprintTrack = humanoid:LoadAnimation(animation)

local function ShiftPressed()
	return UserInputService:IsKeyDown(Key)
end

local function Input(input, gameProcessedEvent)
	if ShiftPressed() and (humanoid.MoveDirection.Magnitude > 0) then
		Tween:Play()
		script.Parent.Humanoid.WalkSpeed = Running_Walkspeed
		sprintTrack:Play()
		humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
			if humanoid.WalkSpeed == 0 then
				sprintTrack:Stop()
			end
		end)
	end	
	end

local function InputEnded(input)
	if input.KeyCode == Key then
		Tween2:Play()
		script.Parent.Humanoid.WalkSpeed = 7
		sprintTrack:Stop()
	end
end



UserInputService.InputBegan:Connect(Input)	
UserInputService.InputEnded:Connect(InputEnded)
1 Like

Any obvious errors in your output/F9 console?

There arent any errors at all.

Use Humanoid | Roblox Creator Documentation instead of detecting walkspeed change. This gives the speed their going at.

1 Like

it worked but now its not playing the animation
it works for a while but then stops playing the animation

humanoid.Running:Connect(function(speed)
	if speed > defaultSpeed and not running then
		--play anim
		running = true
	elseif speed <= defaultSpeed and running  then
		running = false
		--stop anim
	end
end)

This should be declared outside the input function.

When they press shift, just change the walkspeed and play the tween. Only play the animation inside the running connection.

edit: The overall problem you’re facing is that the walkspeed never goes below 0. So the animation is never stopped.

Change the code for this and see if it works

local Running_Walkspeed = 20
local playersService = game:GetService("Players")
local Key = Enum.KeyCode.LeftShift
local Camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local TweenInformation = TweenInfo.new(1) 
local Tween = game:GetService("TweenService"):Create(Camera,TweenInformation,{FieldOfView=95})
local Tween2 = game:GetService("TweenService"):Create(Camera,TweenInformation,{FieldOfView=70})
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
--local animation = script:WaitForChild("Anim")
--local sprintTrack = humanoid:LoadAnimation(animation)

local function ShiftPressed()
	return UserInputService:IsKeyDown(Key)
end

local function Input(input, gameProcessedEvent)
	if ShiftPressed() and (humanoid.MoveDirection.Magnitude > 0) then
		Tween:Play()
		script.Parent.Humanoid.WalkSpeed = Running_Walkspeed
		--sprintTrack:Play()
		humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
			if humanoid.WalkSpeed == 0 then
				--sprintTrack:Stop()
			end
		end)
	end	
end

local function InputEnded(input)
	if input.KeyCode == Key then
		Tween2:Play()
		script.Parent.Humanoid.WalkSpeed = 7
		--sprintTrack:Stop()
	end
end



UserInputService.InputBegan:Connect(Input)	
UserInputService.InputEnded:Connect(InputEnded)

All I did was get rid of all the animation stuff, and it seems to work fine for me.

Well yes, that would work but the whole problem is the running animation not stopping. That will just speed up the default walking animation.

worked but it seems to do this


whenever u go to the left and the right it just restarts the animation

Hmm try changing the comparisons to something like:

if speed >= runSpeed and not running then
	--play anim
	running = true
elseif speed <= defaultSpeed and running  then
	running = false
	--stop anim
end
1 Like

does pretty much the same, is the animation the problem here?

nvm, seems to be the script’s problem, strange

Maybe try something like?:

if speed > defaultSpeed + .2 and not running then
		running = true
	elseif speed <= defaultSpeed + .1 and running then
		running = false
	end

nothing seems to work, i’ve tried a few solutions myself and it stays the same
this might be a roblox bug but i never saw this happening in other games and i never had something like this happening.

Try printing the speed variable. I don’t think it’s exact.

Also, you’re not playing with the animation anywhere else and the priority is action right.

i already tried printing the speed variable, it printed this:
image

well this seems like something that’ll take a while to debug, is this really important to work on because it isnt that noticable and im not quite sure

What does the rest of your script look like?

this is the entire script:

local Running_Walkspeed = 20
local playersService = game:GetService("Players")
local Key = Enum.KeyCode.LeftShift
local Camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local TweenInformation = TweenInfo.new(1) 
local Tween = game:GetService("TweenService"):Create(Camera,TweenInformation,{FieldOfView=95})
local Tween2 = game:GetService("TweenService"):Create(Camera,TweenInformation,{FieldOfView=70})
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animation = script:WaitForChild("Anim")
local sprintTrack = humanoid:LoadAnimation(animation)
local normalSpeed = 7

local function ShiftPressed()
	return UserInputService:IsKeyDown(Key)
end

local function Input(input, gameProcessedEvent)
	if ShiftPressed() and (humanoid.MoveDirection.Magnitude > 0) then
		Tween:Play()
		script.Parent.Humanoid.WalkSpeed = Running_Walkspeed
		sprintTrack:Play()
		humanoid.Running:Connect(function(speed)
			if speed > normalSpeed + .2 and not running then
				print (speed)
				running = true
				sprintTrack:Play()
			elseif speed <= normalSpeed + .1 and running then
				print (speed)
				running = false
				sprintTrack:Stop()
			end
			end)
		end
	end
local function InputEnded(input)
	if input.KeyCode == Key then
		Tween2:Play()
		script.Parent.Humanoid.WalkSpeed = 7
		sprintTrack:Stop()
	end
end



UserInputService.InputBegan:Connect(Input)	
UserInputService.InputEnded:Connect(InputEnded)

One solution might be to use os.clock to only change the animation if the speed has remained the same for a set length of time.

how would i use os.clock in a script like this?