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

TextColor for each player on server?

LocalScript

local player = game.Players.LocalPlayer

local replicatedStorage = game:GetService("ReplicatedStorage")
local eventsFolder = replicatedStorage:WaitForChild("EventsFolder")
local updateEvent = eventsFolder:WaitForChild("UpdateEvent")

local frame = script.Parent
local labels = {}

for i = 1, 10 do
    labels[i] = frame:WaitForChild("Player" .. i)
end

function update()
    local players = game.Players:GetPlayers()
    for number = 1, #labels do
        labels[number].Text = tostring(players[number] or "")
		labels[number].TextColor3 = players[number].TeamColor.Color -- Error is here
    end
end

update()
game.Players.PlayerAdded:connect(update)
game.Players.PlayerRemoving:connect(update)
updateEvent.OnClientEvent:connect(function()
	update()
end)

What I am trying to get to happen is make each label a different color, based on the players TeamColor, but it prints this error in the output: attempt to index field '?' (a nil value) It currently adds a players name to the list, so that's all working, but I just need it to work so the color of the text is the color of each players team.

If I changed players[numbers] to just player it makes all the TextLabels the color of the local player

1 answer

✓ Accepted answer

You have 10 labels and a varying number of player, you try to set each labels properties for all 10 labels regardless of the number of players.

Since the player is not found on line 18 it will not have any of the properties. You need to check if you have a player:-

local player = game.Players.LocalPlayer

local replicatedStorage = game:GetService("ReplicatedStorage")
local eventsFolder = replicatedStorage:WaitForChild("EventsFolder")
local updateEvent = eventsFolder:WaitForChild("UpdateEvent")

local frame = script.Parent
local labels = {}

for i = 1, 10 do
    labels[i] = frame:WaitForChild("Player" .. i)
end

function update()
    local players = game.Players:GetPlayers()
    for number = 1, #labels do

	-- check for a player
	local plr = players[number]
	if plr then -- we have a player
		labels[number].Text = plr.Name
		labels[number].TextColor3 = plr.TeamColor.Color 
	else
		-- no player
		labels[number].Text = ''
		labels[number].TextColor3 = -- some def color
	end
    end
end

update()
game.Players.PlayerAdded:connect(update)
game.Players.PlayerRemoving:connect(update)
updateEvent.OnClientEvent:connect(function()
    update()
end)

I hope this helps.

1 comment

0

Some of it works. Only problem, is for 1 player, it works fine, but for the other player, player1, it only shows player1 with a color, and player2 is just white

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