Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need help with my animation script It only works if I press J once, not multiple times any help?

I need help with my animation script its a keybind one. When I press J the first time everything works but if I try again it says "LoadAnimation requires an Animation object", and it says the error is on line 15. Here is my script:

local UIS = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()

local Key = 'J'

local Animation = Instance.new('Animation')
Animation.AnimationId = 'rbxassetid://4594975382'

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	local KeyPressed = Input.KeyCode
	if KeyPressed == Enum.KeyCode[Key] then
		print(plr.Name..' has pressed'..Key)
		local Load Animation = Char.Humanoid:LoadAnimation(Animation)
		Load = Animation:Play()
	end
end)

1 answer

✓ Accepted answer

Try this,

local UIS = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()

local Key = 'J'

local Animation = Instance.new('Animation')
Animation.AnimationId = 'rbxassetid://4594975382'

UIS.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    local KeyPressed = Input.KeyCode
    if KeyPressed == Enum.KeyCode[Key] then
        print(plr.Name..' has pressed'..Key)
        local Animation = Char.Humanoid:LoadAnimation(Animation)
        Animation:Play()
    end
end)

I changed these lines from this:

local Load Animation = Char.Humanoid:LoadAnimation(Animation)
Load = Animation:Play()

To this:

local Animation = Char.Humanoid:LoadAnimation(Animation)
Animation:Play()

You can't have spaces in variables and there's no need to set Load = Animation:Play() that does nothing. Animation refers to the loaded animation inside the character and you simply play the animation.

1 comment

0

Thanks so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.