Hello, fellow Robloxians! I’d like to ask probably a simple question to many, but, I’m having issues with this “hopefully” simple problem!
What do you want to achieve? I’d like to achieve a solution on my running animation playing whilst idle and pressing “shift”
What is the issue? Whenever I press “shift” on my keyboard, it plays the animation and doesn’t run. But, I don’t want it to do this when I press “shift” whilst being idle
What solutions have you tried so far? I’ve tried using other if statements to check if the player is walking or not
local Players = game:FindService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local anim = Instance.new('Animation')
anim.AnimationId = 'rbxassetid://9483493774'
local playAnim = Humanoid:LoadAnimation(anim)
playAnim:Play()
Humanoid.Running:Connect(function(speed)
if speed > 0.1 then -- Walking
running = true
print('Walking')
else -- Idle
running = false
print('Idle')
playAnim:Stop()
end
end)
another snippet of code inside of a input method using UserInputService
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
playAnim:Play()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25
repeat wait () until running == false
playAnim:Stop()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 15
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
end
I actually use a system similar to this for my custom movement animations except I check when the MoveDirection of the Humanoid changes.
I know we aren’t supposed to spoon-feed scripts, but this is just an example. And you might have to edit it to work for you, and your specific system.
Now I’m not sure exactly what you mean when you say, “Whilst Idle and pressing shift key” I just find Pressing shift key as a keybind to run, So I’m going based off of the first snippet of code where it says “Walking”.
local Player = Game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local WalkTrack = Humanoid:LoadAnimation(script:WaitForChild("Movement").Walking)
-- Animation Settings --
WalkTrack.Priority = Enum.AnimationPriority.Movement
WalkTrack.Looped = true
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if Humanoid.MoveDirection.X ~= 0 or Humanoid.MoveDirection.Z ~= 0 then -- When moving --
if not WalkTrack.IsPlaying then -- Don't restart animation --
WalkTrack:Play() -- Play Animation--
end
else
if Humanoid.MoveDirection.Y ~= 0 then return end -- For the animation to not stop when Jumping and moving --
WalkTrack:Stop() -- Not moving, therefore stops the animation --
end
end)
Thank you! However, I’m slightly confused on where to put it inside of a method.
Code:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local anim = Instance.new('Animation')
anim.AnimationId = "rbxassetid://9483493774"
local WalkTrack = Humanoid:LoadAnimation(anim)
-- Animation Settings --
WalkTrack.Priority = Enum.AnimationPriority.Movement
WalkTrack.Looped = true
game:GetService('UserInputService').InputBegan:Connect(function(process, input)
if process then return end
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if Humanoid.MoveDirection.X ~= 0 or Humanoid.MoveDirection.Z ~= 0 then -- When moving --
if not WalkTrack.IsPlaying then -- Don't restart animation --
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
wait()
end
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 25
WalkTrack:Play() -- Play Animation--
end
else
if Humanoid.MoveDirection.Y ~= 0 then return end -- For the animation to not stop when Jumping and moving --
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 15
for i = 1,5 do
game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
wait()
end
WalkTrack:Stop() -- Not moving, therefore stops the animation --
end
end)
end
end)
The whole system is mainly used for walking, I would say to have the
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
inside of
if Humanoid.MoveDirection.X ~= 0 or Humanoid.MoveDirection.Z ~= 0 then -- When moving --
With an else statement being on the end of the 1st code snippet, Which would be when the Player is Running. Below the MoveDirection.Y at the bottom is when the player stops moving. That is why I’m saying not to have the WalkSpeed be changed there. But instead inside the else statment of input.KeyCode
I would personally use the HumanoidRootParts Velocity instead of the Humanoids MoveDirection, as the MoveDirection X and be 1, while you arent moving at all, because you are walking into a wall.