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

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

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

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)

4 comments

0

The output doesn't show any errors but the animation won't Play?

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

0

But it doesn't so what should I do?

0

He answered your first question. Ask your current question as a new one for best results.

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