I want to make an animation to play when you press F, but if you press it again the animation stops.
How can I do this?
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
if key == "f" then
local db = false
local hum = script.Parent:WaitForChild("Humanoid")
local humanim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))
humanim:Play()
humanim.Looped = true
end
end)
Put this in a Local script inside of StarterCharacterScripts
local animplaying = false
repeat wait() until script.Animation -- Change to Name of Animation
local hum = script.Parent:WaitForChild("Humanoid")
local humanim = hum:LoadAnimation(script.Animation) -- Change to Name of Animation
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
if key == "f" then
if animplaying == false then
animplaying = true
humanim:Play()
humanim.Looped = true
else
animplaying = false
humanim:Stop()
end
end
end)
Maybe make the animation looped. I tested it with a looped animation that I made and it worked with no problems. But then again the “humanim.Looped = true” should have fixed that.
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
if key == "f" then
if db then
local db = false
local hum = script.Parent:WaitForChild("Humanoid")
local humanim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))
humanim:Play()
humanim.Looped = true
end
else if not db then
humananim:stop()
end
end)
Hey, I recommend you to use UserInputService for good practice. Anyways, you should use a variable to determine if the animation is playing, and stop the animation if the variable is true.
local UIS = game:GetService('UserInputService')
local Key = 'F'
local Animation, AnimationPlaying = nil, false
UIS.InputBegan:Connect(function(Input, UI_Interacting)
if Input.KeyCode == Enum.KeyCode[Key] then
if AnimationPlaying then
Animation:Stop() -- This might not be correct.
else
AnimationPlaying = true
Animation:Play()
Animation.Stopped:Wait()
AnimationPlaying = false
-- Start animation
end
end
end)
It says animation is not a valid member of keybind, even tho it is?
local UIS = game:GetService('UserInputService')
local Key = 'F'
local Animation, AnimationPlaying = nil, false
local hum = script.Parent:WaitForChild("Humanoid")
local animation = hum:LoadAnimation(script.Animation) -- error occurs here
UIS.InputBegan:Connect(function(Input, UI_Interacting)
if Input.KeyCode == Enum.KeyCode[Key] then
if AnimationPlaying then
animation:Stop() -- This might not be correct.
else
AnimationPlaying = true
animation:Play()
animation.Stopped:Wait()
AnimationPlaying = false
-- Start animation
end
end
end)
Edit: Try adding print statements wherever possible, like this:
local UIS = game:GetService('UserInputService')
local Key = 'F'
local animation, AnimationPlaying = nil, false
local hum = script.Parent:WaitForChild("Humanoid")
local animation = hum:LoadAnimation(script.Animation)
UIS.InputBegan:Connect(function(Input, UI_Interacting)
if Input.KeyCode == Enum.KeyCode[Key] then
print('Key pressed.')
if AnimationPlaying then
print('Animation was playing. Stopping animation.')
animation:Stop() -- This might not be correct.
else
print('Playing animation.')
AnimationPlaying = true
animation:Play()
animation.Stopped:Wait()
AnimationPlaying = false
-- Start animation
end
end
end)