Hi, I recently made an animation and coded it so that it would play on the key pressed “Q”. I only know how to make this animation run in an if loop and not a single function or to stop the animation after it it finished playing. Here is the script,
local UserInputService = game:GetService("UserInputService")
local Animation = script.Animation
UserInputService.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.Q then
local player = game.Players.LocalPlayer
local character = player.Character
local Humanoid = character:WaitForChild("Humanoid")
local Anim = Humanoid:LoadAnimation(Animation)
Anim:Play()
end
end)
Everything works except I would like the animation to run only once when Q is clicked.
local UserInputService = game:GetService("UserInputService")
local Animation = script.Animation
local didPlay = false
UserInputService.InputBegan:Connect(function(Input)
if didPlay then return end
if Input.KeyCode == Enum.KeyCode.Q then
local player = game.Players.LocalPlayer
local character = player.Character
local Humanoid = character:WaitForChild("Humanoid")
local Anim = Humanoid:LoadAnimation(Animation)
Anim.Play()
didPlay = true
end
end)
Just make it so your animation isn’t looped, try this:
local UserInputService = game:GetService("UserInputService")
local Animation = script.Animation
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local Anim = Humanoid:LoadAnimation(Animation)
Anim.Looped = false
UserInputService.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.Q then
Anim:Play()
end
end)