So I was trying to make it play an animation when ever the player is pressing A or D but something is not right it is not printing anything when I press one of those keys. Can someone help me with this???
local UserInputService = game:GetService("UserInputService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local rep = game:GetService("ReplicatedStorage")
local eventa = rep:FindFirstChild("Events"):FindFirstChild("Animations"):WaitForChild("Left")
local eventb = rep:FindFirstChild("Events"):FindFirstChild("Animations"):WaitForChild("Right")
local endeventa = rep:FindFirstChild("Events"):FindFirstChild("Animations"):WaitForChild("LeftEnd")
local endeventb = rep:FindFirstChild("Events"):FindFirstChild("Animations"):WaitForChild("RightEnd")
local animationA = humanoid.Animator:LoadAnimation(script.AnimationA)
local animationB = humanoid.Animator:LoadAnimation(script.AnimationB)
local isPlayingA = false
local isPlayingB = false
local function playAnimationA()
if not isPlayingA then
print("--Playing Animation | A")
animationA:Play()
eventa:FireServer()
isPlayingA = true
end
end
local function playAnimationB()
if not isPlayingB then
print("--Playing Animation | B")
animationB:Play()
eventb:FireServer()
isPlayingB = true
end
end
local function stopAnimationA()
if isPlayingA then
print("--Ending Animation | A")
animationA:Stop()
endeventa:FireServer()
isPlayingA = false
end
end
local function stopAnimationB()
if isPlayingB then
print("--Ending Animation | B")
animationB:Stop()
endeventb:FireServer()
isPlayingB = false
end
end
local function playstandartAnimation()
print("--Playing Animation | Standart")
end
local function stopplayingstandartanimation()
print("--Ending Animation | Standart")
end
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
print("playing animationA | KEY PRESSED: [A]")
playAnimationA()
elseif input.KeyCode == Enum.KeyCode.D then
print("playing animationB | KEY PRESSED: [D]")
playAnimationB()
end
end)
UserInputService.InputEnded:Connect(function(input)
print("ending animationA")
if input.KeyCode == Enum.KeyCode.A then
stopAnimationA()
elseif input.KeyCode == Enum.KeyCode.D then
print("ending animationB")
stopAnimationB()
end
end)
And yea it is in the StarterPlayerScripts and it is a localscript
You don’t need an isPlayingB or isPlayingA variable. Start the animation when they hold the key down and end it when they let go.
Code:
local UserInputService = game:GetService("UserInputService")
local rep = game:GetService("ReplicatedStorage")
local eventa = rep.Events.Animations:WaitForChild("Left")
local eventb = rep.Events.Animations:WaitForChild("Right")
local endeventa = rep.Events.Animations:WaitForChild("LeftEnd")
local endeventb = rep.Events.Animations:WaitForChild("RightEnd")
local humanoid = script.Parent:WaitForChild("Humanoid")
local animationA = humanoid.Animator:LoadAnimation(script.AnimationA)
local animationB = humanoid.Animator:LoadAnimation(script.AnimationB)
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
animationA:Play()
eventa:FireServer()
elseif input.KeyCode == Enum.KeyCode.D then
animationB:Play()
eventb:FireServer()
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
animationA:Stop()
endeventa:FireServer()
elseif input.KeyCode == Enum.KeyCode.D then
animationB:Stop()
endeventb:FireServer()
end
end)
Oh okay! I wrote a print function into the function that should know if the user pressed the key but nothing happends for some reason I am still confused.
I’m not sure on exactly why it’s not working. All I know is that A and D are reserved keybinds when the character can actively move. I’m assuming your using this to tell when to play strafing left/right animations? If that’s the case, try this instead:
1: Get the LookVector of the character, set it to look 2: Fix look by removing it’s Y component 3: Set speed to look.Magnitude * hum.WalkSpeed 4: Set move to hum.MoveDirection 5: Get the strafe angle based on the look direction using math.acos(math.deg(move:Dot(look))(fact check me on this) 6: Use the angle to determine which of the 8 degrees of motion the user is walking in 7: Play the correct strafing animation based on the angle 8: Adjust the animation speed using :AdjustSpeed(speed / 16)