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

How do I stop an error from occuring? (see script below)

I have this script in ServerScriptService:

game.Players.PlayerAdded:Connect(function(player)
	local cframeStored
	
	player.CharacterAdded:Connect(function(character)
		local player = game.Players.LocalPlayer
		
		character:WaitForChild("Humanoid").Died:Connect(function()
	
			if player.Team == "Prisoner" then 
				cframeStored = CFrame.new(game.Workspace.Prisoner.Position + Vector3.new(0, 10, 0))
			end 
		end)
	end)
end)

Whenever I play my game, Roblox outputs with this error message:

ServerScriptService.TeamSpawnLocation:9: attempt to index local 'player' (a nil value)

How do I fix this problem?

Any help is appreciated :)

5 comments

1

Remove Line 5, LocalPlayer can't be used in ServerScripts. That's why it's throwing an error.

0

ty it works but do you know how to make line 9 - 10 work as well?

0

Check my answer

0

Lines 9 and 10 should still work if you remove line 5 because the player is set above on your first line. To fix line 9, you should be doing "player.Team.Name == "Prisoner" "

0

That doesn't seem to be working, When the player dies, it doesn't spawn to line 10 location.

1 answer

✓ Accepted answer

The problem is that you are having local player in a server script instead use the parameter of the player added

Server Script:

game.Players.PlayerAdded:Connect(function(player) -- The player is given here
        local CFrameStored

        player. CharacterAdded:Connect(function(character)
                character:WaitForChild("Humanoid").Died:Connect(function()
                         if player.Team.Name == "Prisoner" then
                               local CFrameStored =  CFrame.new(game.Workspace.Prisoner.Position + Vector3.new(0,10,0))
                         end) 
                 end) 
        end) 
end)

If you see any mistakes it's because I'm on mobile

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