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

How can I reference the local player from a regular script?

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()

1 answer

✓ Accepted answer

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

1 comment

0

Thank you so much, this worked perfectly!

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