I’ve been trying multiple times in order to try to play the animation in this shift to sprint. I’d would also love to make a check in order to stop the animation when the player’s movement is halted.
Here is my script:
local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")
local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25
local function startSprinting(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
characterHumanoid.WalkSpeed = boostedSpeed
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
end
end
local function stopSprinting(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
characterHumanoid.WalkSpeed = normalSpeed
end
end
UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)
Load the animation into the animator which is found inside of the humanoid.
local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local Anim = Animator:LoadAnimation(animation)
Anim:Play() -- To Play
Anim:Stop()-- To Stop
Important
Do not LoadAnimation every time they sprint, instead make it when the script runs.
You should use contextactionservice instead of userinputservice because userinputservice triggers while you’re typing and you can’t make mobile buttons for it.
local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")
local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25
local function startSprinting(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
characterHumanoid.WalkSpeed = boostedSpeed
local Anim = animation:LoadAnimation(animation)
Anim:Play() -- To Play
Anim:Stop()-- To Stop
end
end
local function stopSprinting(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
characterHumanoid.WalkSpeed = normalSpeed
end
end
UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)
No. That is gonna both play and stop. You need InputBegan and InputEnded which you should used in your original script to change walk speed. Also please, use contextactionservice. It’s better than userinputservice. You won’t have to rewrite much.
I only know how to use uis, not cts.
I’m also not familiar with InputBegan or InputEnded, do you mind explaining? (I’m pretty new to lua, so it’s a bit difficult to understand everything.)
local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")
local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25
local Anim = animation:LoadAnimation(animation)
local function startSprinting(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
characterHumanoid.WalkSpeed = boostedSpeed
Anim:Play() -- To Play
end
end
local function stopSprinting(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
characterHumanoid.WalkSpeed = normalSpeed
Anim:Stop()-- To Stop
end
end
UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)
local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")
local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25
local Anim = characterHumanoid:WaitForChild("Animator"):LoadAnimation(animation)
local function startSprinting(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
characterHumanoid.WalkSpeed = boostedSpeed
Anim:Play() -- To Play
end
end
local function stopSprinting(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
characterHumanoid.WalkSpeed = normalSpeed
Anim:Stop()-- To Stop
end
end
UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)
I’m confused by this. I’ve seen another post discussing something like this and the solution includes that line of code, and the creator marked it as a solution.