Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
natl1 2
5 years ago

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

Log in to vote
0
Answered by
xdeno 187
5 years ago

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.

0
Thanks so much! natl1 2 — 5y
Ad

Answer this question