I am trying to make my shift to sprint script do the following:
Not override jump animation
Not work while not moving
And I have been having trouble trying to achieve this. Here’s my code below:
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
if Character.Humanoid.MoveDirection.Magnitude > 0 then
Character.Humanoid.WalkSpeed = 24
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://8316922787'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
elseif Character.Humanoid.MoveDirection.Magnitude <= 0 then
PlayAnim:Stop()
end
end
end)
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 16
PlayAnim:Stop()
end
end)
local YourAnimation = "http://www.roblox.com/asset/?id=252557606" -- Your run anim
local ShiftSpeed = 30 -- Speed will be added
local OriginalSpeed = 16 -- Original speed
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
repeat
wait()
until
Player.Character
local Character = Player.Character
local RunAnimation = Character:WaitForChild("Animate").run:GetChildren()[1]
local OrginalAnimation = Instance.new("StringValue", Character)
OrginalAnimation.Value = RunAnimation.AnimationId
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
RunAnimation.AnimationId = YourAnimation
Character:WaitForChild("Humanoid").WalkSpeed = ShiftSpeed
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
RunAnimation.AnimationId = OrginalAnimation.Value
Character:WaitForChild("Humanoid").WalkSpeed = OriginalSpeed
end
end)
I republished the animation (with the animation priority still set to action) and it didn’t work.
I have the script set as a LocalScript in StarterGUI since that was where my last script was.
When I try your code it gives an error because you got the player’s character too early. You had to do Player.CharacterAdded:Wait() and then get the player’s character.
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
Player.CharacterAdded:Wait()
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Jumping = false
local Animation = Instance.new("Animation")
Animation.AnimationId = "" -- Insert The Sprint Animation Here. Your Animation Id doesn't seem to be working.
local Animation = Humanoid:LoadAnimation(Animation)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift and not Jumping then
Animation:Play()
Humanoid.WalkSpeed = 32
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift and not Jumping then
Animation:Stop()
Humanoid.WalkSpeed = 16
end
end)
Humanoid.Jumping:Connect(function(boolean)
Jumping = boolean
end)
Make a new template in Roblox Studio and add the code into a local script inside the Starter Pack folder. There might be a script interfering with the code in your game.
Edit: The code will only work if you put an animation id between the strings next to the Animation.AnimationId variable.
Well I put it inside a new game and I realized it didn’t work with StarterGui, I put the script in a new template, and then I put it in my original (obviously in StarterPlayerScripts), they both had the animations working but it still has the same problems I was trying to solve.
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
Player.CharacterAdded:Wait()
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "" -- Insert The Sprint Animation Here. Your Animation Id doesn't seem to be working.
local Animation = Humanoid:LoadAnimation(Animation)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Animation:Play()
Humanoid.WalkSpeed = 32
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Animation:Stop()
Humanoid.WalkSpeed = 16
end
end)
Humanoid.Jumping:Connect(function(jumping)
if jumping and Animation.IsPlaying then
Animation:Stop()
end
end)
Humanoid.Running:Connect(function(speed)
if speed == 0 and Animation.IsPlaying then
Animation:Stop()
end
end)
It worked! Thank you so much, the only thing I need to fix is that after you jump the sprinting still works, but the animation stops, I can fix it though. Thank you all for your help!
oh mb ye values does not works enough for saving sorry for lazy scripting
local YourAnimation = "http://www.roblox.com/asset/?id=656118852" -- This is ninja run anim pack change it to your run anim just for check
local ShiftSpeed = 30 -- Speed will be added
local OriginalSpeed = 16 -- Original speed
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
repeat
wait()
until
Player.Character
local Character = Player.Character
local RunAnimation = Character:WaitForChild("Animate").run:GetChildren()[1]
local Humanoid = Character:WaitForChild("Humanoid")
local OrginalAnimation = Humanoid:GetAppliedDescription().RunAnimation
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
RunAnimation.AnimationId = YourAnimation
Character:WaitForChild("Humanoid").WalkSpeed = ShiftSpeed
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
RunAnimation.AnimationId = "http://www.roblox.com/asset/?id="..OrginalAnimation
Character:WaitForChild("Humanoid").WalkSpeed = OriginalSpeed
end
end)