I was working on a project and the script would work fine up until it got to the part where it needed to reference the local player. I am trying to change the teams of the players and it worked when I had the script inside the PlayerGui as I could reference the parent four times, however, whenever the player died it would reset the script. To fix this I moved it outside of the PlayerGui and into the workspace. It would also come up with the error message that TeamColor could not be found. The script found below is only part of it and the rest of the script works just fine.
local player = game.Players.LocalPlayer player.TeamColor = BrickColor.new("Cyan") wait(0.1) local randomPlayer = game.Players:GetPlayers() [math.random(1,#game.Players:GetPlayers())] randomPlayer.TeamColor = BrickColor.new("Crimson") wait(0.1) player:LoadCharacter()
You can use the PlayerAdded function or iterate through players using a for loop.
local players = game:GetService("Players") players.PlayerAdded:Connect(function(player) --code end) for i,v in pairs(players:GetPlayers()) do --v is the player in this case end