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

Attempt to index local 'player' (a nil value)?

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

✓ Accepted answer

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)

`

2 comments

0

thanks :D +1 and accepted :D

0

It would be easier to use a CharacterAdded event.

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