(HELP NEEDED) Need help with sprint script animations

I have a sprint script I made and it works fine except for when you run and then stop if your still holding shift it still plays the animation and sounds and stuff, Please Help!

local plr = game:GetService("Players").LocalPlayer
local character = plr.Character
local Humanoid = character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Camera = workspace:WaitForChild("Camera")

local footstepSound = Instance.new("Sound")
local HumanoidRootPart = plr.Character:WaitForChild("HumanoidRootPart")
local Animation = Instance.new('Animation')
local animator = Humanoid:WaitForChild("Animator")

footstepSound.SoundId = "rbxassetid://944075408"
footstepSound.Pitch = 1.8
footstepSound.Parent = HumanoidRootPart
footstepSound.Looped = true

-- configuration
local walkspeed = 6
local sprintspeed = 24
local key = Enum.KeyCode.LeftShift
Animation.AnimationId = "rbxassetid://18522167288"
local runanim = animator:LoadAnimation(Animation)

UIS.InputBegan:Connect(function(Key, gameProcessed)
	if gameProcessed then return end
	local LastPosition = HumanoidRootPart.Position
	if Key.KeyCode == key then
		repeat wait() until HumanoidRootPart.Position ~= LastPosition
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 90}):Play()
		Humanoid.WalkSpeed = sprintspeed
		footstepSound:Play()
		runanim:Play()
	end
end)

UIS.InputEnded:Connect(function(Key, gameProcessed)
	if gameProcessed then return end
	if Key.KeyCode == key then
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 70}):Play()
		Humanoid.WalkSpeed = walkspeed
		footstepSound:Stop()
		runanim:Stop()
	end
end)


set all wait() to task.wait(), try removing the gameprocessed code piece from the inputended function

you arent ending your input when you stop. if you want your run animation, and sounds to stop try detecting if the player is moving or not

Yeah iā€™ve been trying to do that for the past 2 hours. Do you know how I could do that? I canā€™t get it to work when i try

function Moving()
	return Humanoid.MoveDirection.Magnitude > 0
end


UIS.InputBegan:Connect(function(Key, gameProcessed)
	if Moving() then
		warn("do stufff")
	end
end)

Script still works fine but the bug is the same

local plr = game:GetService("Players").LocalPlayer
local character = plr.Character
local Humanoid = character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Camera = workspace:WaitForChild("Camera")

local footstepSound = Instance.new("Sound")
local HumanoidRootPart = plr.Character:WaitForChild("HumanoidRootPart")
local Animation = Instance.new('Animation')
local animator = Humanoid:WaitForChild("Animator")

footstepSound.SoundId = "rbxassetid://944075408"
footstepSound.Pitch = 1.8
footstepSound.Parent = HumanoidRootPart
footstepSound.Looped = true

-- configuration
local walkspeed = 6
local sprintspeed = 24
local key = Enum.KeyCode.LeftShift
Animation.AnimationId = "rbxassetid://18522167288"
local runanim = animator:LoadAnimation(Animation)

function Moving()
	return Humanoid.MoveDirection.Magnitude > 0
end


UIS.InputBegan:Connect(function(Key, gameProcessed)
	if Key.KeyCode == key then
		if Moving() then
			TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 90}):Play()
			Humanoid.WalkSpeed = sprintspeed
			footstepSound:Play()
			runanim:Play()
		end
	end
end)


UIS.InputEnded:Connect(function(Key, gameProcessed)
	if Key.KeyCode == key then
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 70}):Play()
		Humanoid.WalkSpeed = walkspeed
		footstepSound:Stop()
		runanim:Stop()
	end
end)

you move while holding shift, you stop moving but keep holding shift, and everything still plays


1 Like

maybe try putting the ā€œif moving()ā€ before the ā€œif keyā€

Iā€™ve fixed the animation error but now when you stop while sprinting even if your still holding the key it doesnā€™t continue sprinting, i know why it doesnā€™t but i am stumped on how to make it DO. Please help!

local plr = game:GetService("Players").LocalPlayer
local character = plr.Character
local Humanoid = character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Camera = workspace:WaitForChild("Camera")

local footstepSound = Instance.new("Sound")
local HumanoidRootPart = plr.Character:WaitForChild("HumanoidRootPart")
local Animation = Instance.new('Animation')
local animator = Humanoid:WaitForChild("Animator")
local Sprinting = false

footstepSound.SoundId = "rbxassetid://944075408"
footstepSound.Pitch = 1.8
footstepSound.Parent = HumanoidRootPart
footstepSound.Looped = true

-- configuration
local walkspeed = 6
local sprintspeed = 24
local key = Enum.KeyCode.LeftShift
Animation.AnimationId = "rbxassetid://18522167288"
local runanim = animator:LoadAnimation(Animation)
local THEKEY = false

local function Sprint()
	TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 90}):Play()
	Humanoid.WalkSpeed = sprintspeed
	footstepSound:Play()
	runanim:Play()
end

local function StopSprint()
	TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 70}):Play()
	Humanoid.WalkSpeed = walkspeed
	footstepSound:Stop()
	runanim:Stop()
end

local function PauseSprint()
	TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 70}):Play()
	Humanoid.WalkSpeed = walkspeed
	footstepSound:Pause()
	runanim:Pause()
end


UIS.InputBegan:Connect(function(Key, gameProcessed)
	if Key.KeyCode == key then
		Sprinting = true		
		THEKEY = key
		Sprint()
	end
end)


UIS.InputEnded:Connect(function(Key, gameProcessed)
	if Key.KeyCode == key then
		Sprinting = false
		StopSprint()
		THEKEY = false
	end
end)

game:GetService("RunService").Heartbeat:Connect(function()
	if Sprinting == true then
		repeat wait() until Humanoid.MoveDirection.Magnitude == 0
		StopSprint()
	end
end)


could you send me a rbxl file with the script

I mean iā€™m not sure the animations will work for you because the place is private but heres a rbxl file with the script

woahhhepic.rbxl (62.5 KB)

Not sure what exactly is the issue, because itā€™s working fine for me. I canā€™t tell exactly because I donā€™t have the animation, however, everything else seemed to work fine with your RunService approach.

When I used to play the run animation manually, this event was always present for me:

Humanoid.Running:Connect(function(speed)
	if speed <= 0 and Sprinting then
		StopSprint()
	end
end)

But, today my sprint is merely just a change in walk speed, because the ā€˜runā€™ animation is played automatically based on the move direction and walk speed. You could say itā€™s dynamic.

basically when you stop moving but still hold shift then start moving again (holding shift) you donā€™t continue running, thats what i want, i want it to continue running, so you donā€™t have to press shift again. Though this problem is with the same script, should i make a new post for what i want added to my script?

Now I understand the problem very well. The reason you stop running is because of the ā€˜InputEndedā€™ event. Whenever you let go of the key that event will get you out of the run state.

What you want to do is to use a toggle value and a single ā€˜InputBeganā€™ event.

local toggle = false

UIS.InputBegan:Connect(function(Key, gameProcessed)
	if Key.KeyCode == key then
		toggle = not toggle
		if toggle then
			Sprint()
		else
			StopSprint()
		end
	end
end)