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 9 years ago
game.Workspace.ChildAdded:connect(function(char)
    local player = game.Players:GetPlayerFromCharacter(char)
    local stats = player.leaderstats
    local spawn = game.Workspace:FindFirstChild(stats.Level.Value)
    char.Torso.CFrame = char.Torso.CFrame + Vector3.new(0,3,0)
    wait()
    char.Torso.CFrame = spawn.CFrame + Vector3.new(0,3,0)
end)

Workspace.MainScript:3: attempt to index local 'player' (a nil value) How can player be nil, from what I see the :GetPlayerFromCharacter() should work. Any help?

1 answer

Log in to vote
3
Answered by
RedCombee 585 Moderation Voter
9 years ago

A character isn't going to be the only thing that is added to the Workspace. An easy way to fix this is to add an if statement that checks to see if the character is actually a character.

game.Workspace.ChildAdded:connect(function(char)
    if game.Players:GetPlayerFromCharacter(char) then
        local player = game.Players:GetPlayerFromCharacter(char)
        local stats = player.leaderstats
        local spawn = game.Workspace:FindFirstChild(stats.Level.Value)
        char.Torso.CFrame = char.Torso.CFrame + Vector3.new(0,3,0)
        wait()
        char.Torso.CFrame = spawn.CFrame + Vector3.new(0,3,0)
    end
end)

`

0
thanks :D +1 and accepted :D NinjoOnline 1146 — 9y
0
It would be easier to use a CharacterAdded event. Perci1 4988 — 9y
Ad

Answer this question