You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want my script so that when I press shift, it increases the walkspeed of the player like sprinting, and plays an animation and when they let go of it, their walkspeed goes back to normal and the animation stops playing.
What is the issue? Include screenshots / videos if possible!
Everything else works, my only problem now is stopping the animation when the player stops moving even if they are holding down shift.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried multiple ways of detecting player movement, but I’ve stuck with magnitude as I think it’s the best way of detecting player movement (?)
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera
local runSpeed = 17
local walkSpeed = 10
local animation = script:WaitForChild("Sprint")
local animTrack = humanoid:LoadAnimation(animation)
local shiftHeld = false -- is shift held down?
-- Function to smoothly transition the field of view
local function transitionFOV(targetFOV)
local currentFOV = camera.FieldOfView
local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local fovTween = game:GetService("TweenService"):Create(camera, tweenInfo, {
FieldOfView = targetFOV
})
fovTween:Play()
end
-- Function to handle transition between sprinting and walking
local function shiftToSprint()
if shiftHeld and humanoid.MoveDirection.Magnitude > 0.1 then
transitionFOV(80)
humanoid.WalkSpeed = runSpeed
animTrack:Play()
print("User started sprinting")
else
transitionFOV(70)
humanoid.WalkSpeed = walkSpeed
animTrack:Stop()
print("User stopped sprinting")
end
end
-- Detect when shift key is pressed
game:GetService("UserInputService").InputBegan:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.LeftShift then
shiftHeld = true
shiftToSprint()
end
end)
-- Detect when shift key is released
game:GetService("UserInputService").InputEnded:Connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.LeftShift then
shiftHeld = false
shiftToSprint()
end
end)
-- Check for changes in Humanoid State
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Running then
shiftToSprint()
else
shiftToSprint()
end
end)
I’m quite puzzled by this. My only thought is that playing the animation track several times stacks, and so that stopping it only stops one of the plays. Though I don’t think this is right.
Try adding prints to shiftToSprint and have it print out shiftHeld. See if it ever prints out false.
You don’t want to play the animation constantly, just check if the player is NOT walking. If he isn’t, then stop it. Only play it once and then run the checks, without anything if the player IS walking. Also you might wanna disconnect that Heartbeat event after you stop the movement.
connection = game["Run Service"].Heartbeat:Connect(checkMovement)
if not running then
connection:Disconnect()
end
I usually like making my own animate scripts, not using the defaults, like this
and my testing script that worked well just now is this
local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local WalkAnimation = script:WaitForChild("Walk")
local SprintAnimation = script:WaitForChild("Sprint")
local IdleAnimation = script:WaitForChild("Idle")
local IdleTrack = Humanoid:LoadAnimation(IdleAnimation)
local WalkTrack = Humanoid:LoadAnimation(WalkAnimation)
local SprintTrack = Humanoid:LoadAnimation(SprintAnimation)
local WalkSpeed = 10
local SprintSpeed = 18
IdleTrack:Play()
local CurrentAnimationTrack = WalkTrack
Humanoid.Running:Connect(function(Speed)
if Speed > 0 then
if not CurrentAnimationTrack.IsPlaying then
CurrentAnimationTrack:Play()
end
CurrentAnimationTrack:AdjustSpeed(Speed/16)
else
if CurrentAnimationTrack.IsPlaying then
CurrentAnimationTrack:Stop()
end
end
end)
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
CurrentAnimationTrack:Stop()
CurrentAnimationTrack = SprintTrack
Humanoid.WalkSpeed = SprintSpeed
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
CurrentAnimationTrack:Stop()
CurrentAnimationTrack = WalkTrack
Humanoid.WalkSpeed = WalkSpeed
end
end)
ofc this doesnt have any climbing or jumping animations and stuff but you can add them
although i did run into an issue, might be on my end however, but I joined the game and the sprint animation works great and the idle too, but the walk animation doesn’t seem to play. I’ve double checked all the animation ids are different, so I’m not sure what the issue is on this one.
i think u should go to the animation editor and make sure that your idle animations priority is set to “idle”,
and walk and sprint animations priorities are set to “movement”