Changing color of the image label to the same color as the team which the player is in

hello,
im trying to make the color of my power bar (which is a image laber) to the same color as the team which the player is in, like if the player is in the blue team, the power bar will beblue, else if the player is in the red team, the power bar will be red.
how can i do that?

In a local script in the Image label put this:

local Players = game:GetService("Players") -- Get the "Players" service
local Player = Players.LocalPlayer -- Get the current client
local TeamColor = Player.TeamColor -- Get the team color

script.Parent.ImageColor3 = TeamColor.Color -- Set the color of the label to the team color's Color3 value

doesnt work, when im the blue team the power bar gets the color, but when i put myself in the red team, the color stays blue

It requires Server to Client communication for this. So start by making a remote event.

Server Script


game.Players.PlayerAdded:Connect(function(player)
   local RE = game:GetService('ReplicatedService').RemoteEvent
   RE:FireClient(player, player.TeamColor)

   player.Team.Changed:Connect(function()
      RE:FireClient(player, player.TeamColor)
   end)
end)

Local Script


local RE = game:GetService('ReplicatedStorage').RemoteEvent

RE.OnClientEvent:Connect(function(Tcolor)
   script.Parent.ImageColor3 = Tcolor.Color
end)

Hope this helps.

A player’s ‘TeamColor’ property can be read from the client (it replicates).

ImageLabel.ImageColor3 = LocalPlayer.TeamColor.Color

He said this doesn’t work. I think was he meant was for it to change automatically when he switches teams. I think it’s better to add a Team.Changed event to recognize whenever the team is changed.

1 Like

It would work, they just need to listen for each player’s ‘Team’/‘TeamColor’ property to change via GetPropertyChangedSignal.

Modified @domboss37 code, this should work

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

spawn(function()
	while wait() do
		script.Parent.ImageColor3 = Player.TeamColor.Color 
	end
end)

You can use GetPropertyChangedSignal aswell

This would just end up causing lag when there are a lot of scripts in the game. This script will keep on running but GetPropertyChangedSignal would be the best solution to this.

I did say he can use it, I just gave him a base idea of what it should like and then said he can use GetPropertyChangedSignal and it indeed is better.

1 Like

i solved this problem on my own by checking if the player is in a specific team the power bar color will change, but thanks to everyone who gave me an help i really needed it!