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

attempt to index local 'Player' (a nil value)?

Asked by
KenzaXI 166
8 years ago

Hi, I made a Animation script for a keydown function and for some reason the Title is what the output returned. Can you help me out please? Here's the script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
repeat wait() until Player.Character and Mouse
Mouse.KeyDown:connect(function(Key)
    if Key == "q" then
        local Animation = Instance.new("Animation")
        Animation.AnimationId = "http://www.roblox.com/Asset?ID=281476213"
        local animTrack = Player.Character.Humanoid:LoadAnimation(Animation)
        animTrack:Play()
    end
end)

Cheers for the help.

1 answer

Log in to vote
2
Answered by
4Bros 550 Moderation Voter
8 years ago

Alright, so your problem is that you referenced a variable to the player before it even existed. You can't reference something that doesn't exist yet and wait until it does exist. You have to do it the opposite way around.

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()  -- The mouse should already exist since the player exists
Mouse.KeyDown:connect(function(Key)
    if Key == "q" then
        local Animation = Instance.new("Animation")
        Animation.AnimationId = "http://www.roblox.com/Asset?ID=281476213"
        local animTrack = Player.Character.Humanoid:LoadAnimation(Animation)
        animTrack:Play()
    end
end)

0
The output doesn't show any errors but the animation won't Play? KenzaXI 166 — 8y
0
I'm not sure if the animation needs to exist within the datamodel to be played, but if it is your animation then you should be able to play it 4Bros 550 — 8y
0
But it doesn't so what should I do? KenzaXI 166 — 8y
0
He answered your first question. Ask your current question as a new one for best results. User#5978 25 — 8y
Ad

Answer this question