local player = game.Players.LocalPlayer
local userinput = game:GetService(“UserInputService”)
local keydown = false
local runanimation = Instance.new(“Animation”)
runanimation.AnimationId = “rbxassetid://youranimationidhere”
runanimation = player.Character.Humanoid:LoadAnimation(runanimation)
userinput.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
keydown = true
end
end)
userinput.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
keydown = true
end
end)
while true do
wait(0.1)
if keydown == true then
runanimation:Play()
else
runanimation:Stop()
end
end
What could I add to this script so that I have to hold W for it to work because when I press left shift it play that animation without me moving so what do I do ?
Okay so the problem with your script is that you are making the keydown variable true for both InputBegan and InputEnded. You just want to make the keydown variable to false when the key is let go (InputEnded).
Also, I do not recommend using a While Do loop, as it can cause unnecessary lag. Instead, just play and stop the animation in the InputBegan and InputEnded functions.
Here is your fixed code:
local player = game.Players.LocalPlayer
local userinput = game:GetService("UserInputService")
local keydown = false
local runanimation = Instance.new("Animation")
runanimation.AnimationId = "rbxassetid://youranimationidhere"
runanimation = player.Character.Humanoid:LoadAnimation(runanimation)
userinput.InputBegan:Connect(function(input, typing)
if typing then
return -- makes sure code doesn't run if player is chatting
end
if input.KeyCode == Enum.KeyCode.LeftShift then
if not keydown then
runanimation:Play()
keydown = true
end
end
end)
userinput.InputEnded:Connect(function(input, typing)
if typing then
return -- makes sure code doesn't run if player is chatting
end
if input.KeyCode == Enum.KeyCode.LeftShift then
if keydown then
runanimation:Stop()
keydown = false
end
end
end)