Auto Assigning Failure

I’m trying to write a simple script that automatically assigns everyone to a team once they join. It keeps failing and the output says: "ServerScriptService.Script:5: attempt to index nil with ‘Team’ "

Here’s the code:

local Player = game.Players.LocalPlayer
local Spectating = game.Teams.Spectating

game.Players.PlayerAdded:Connect(function()
	Player.Team = Spectating
end)

Players.LocalPlayer is the Player instance associated with the client, but you’re using a server-sided script which the LocalPlayer does not exist on. Instead, you can use the player argument in the PlayerAdded event to get the player.

local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

local Spectating = game.Teams.Spectating

Players.PlayerAdded:Connect(function(Player)
	Player.Team = Spectating
end)

game.Players.PlayerAdded:Connect(function(Player)

Player.Team = Spectating.Spectating

end)

Works and explained well! Thanks!