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)
function Moving()
return Humanoid.MoveDirection.Magnitude > 0
end
UIS.InputBegan:Connect(function(Key, gameProcessed)
if Moving() then
warn("do stufff")
end
end)
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
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)
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)