Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago

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
0
Thank you so much, this worked perfectly! Creeperbob16 28 — 4y
Ad

Answer this question