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?
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)
`