Index string with TextColor

I’m modifying a rank UI script and changing the TextColour of the text when it displays a certain message, but I can’t because it comes up with this error:

It errors on the lines where I set the TextColor of v.Name.

Any help would be appreciated.

You are trying to set TextColor to the Group Name which is only a string got from a :GetGroupsAsync() method. Because TextColor is a property in a TextLabel You need to set it while creating that Label.

What lines should I set the TextColor then?

--[...] Lines 67-72
for i,v in pairs(Divs) do
   local t = Template:Clone()
   --You need do do this inside that for, after You cloned the template
   t.Parent = Tip.Divisions
   t.Position = UDim2.new(0, 0, 0, 10 * (i - 1))
   t.Text = v
 end
--[...]

So there I do

if t.Text == "..." then
    t.TextColor = Color3.fromRGB(...)

Technically Yes, but if you have in Text a Group Name + Group Rank You’ll need to use string.find to find the name of group or You can modify the GetUserDivs function to return Text and TextColor

How should I use string.find?

if t.Text == string.find("[RAF] Royal Air Force Police") then
	t.Text = "RAF Police"
	t.TextColor = Color3.fromRGB(25, 175, 255)

No, you need to pass to the function 2 strings, first the string that will search for a 2nd string which in your case will be group name. And You do group name conversion in the GetUserDivs function.

--For example t.Text == "RAF Police - [TR] Trainee Constable"
if string.find(t.Text, "RAF Police") then
   t.TextColor = Color3.fromRGB(255,255,255)
end

I’ve just put your reply into context, does this look right?

mouse.Move:connect(function()	
	if mouse.Target then
		local tPlayer = game:service("Players"):GetPlayerFromCharacter(mouse.Target.Parent)
		if tPlayer then
						Tip.Visible = true
						Tip.Divisions.Visible = true
						CurrentTarget = tPlayer
						local Divs = GetUserDivs(tPlayer)
						Tip.Divisions:ClearAllChildren()
						Tip.Text = tPlayer.Name
						for i,v in pairs(Divs) do
						local t = Template:Clone()
							t.Parent = Tip.Divisions
							t.Position = UDim2.new(0, 0, 0, 10 * (i - 1))
							t.Text = v
				
						if t.Text == "[RAF] Royal Air Force Police" then
								t.Text = "RAF Police"
						elseif
							t.Text == "RAF || The Royal Air Force" then
								t.Text = "Royal Air Force"
						elseif
							t.Text == "[RAF] No 1 group" then
								t.Text = "No II"
						else
							if t.Text == "[RAF] Royal Air Force Regiment" then
								t.Text = "RAF Regiment"
								t.TextColor = Color3.fromRGB(10, 170, 130)
						end

						wait(.1)
						if string.find(t.Text, "RAF Police") then
							t.TextColor = Color3.fromRGB(25, 175, 255)
						elseif
							string.find(t.Text, "Royal Air Force") then
								t.TextColor = Color3.fromRGB(10, 110, 255)
						elseif
							string.find(t.Text, "No II") then
								t.TextColor = Color3.fromRGB(50, 100, 225)
						else
							if string.find(t.Text, "RAF Regiment") then
								t.TextColor = Color3.fromRGB(10, 170, 130)
							end
						end
					end
				end
			else
				Tip.Visible = false
		end
	end
end)

Not quite. If You didn’t make changes to the GetUserDivs function it already returns a table containing a string like "RAF Police - [TR] Trainee Constable"

mouse.Move:connect(function()	
	if mouse.Target then
		local tPlayer = game:service("Players"):GetPlayerFromCharacter(mouse.Target.Parent)
		if tPlayer then
						Tip.Visible = true
						Tip.Divisions.Visible = true
						CurrentTarget = tPlayer
						local Divs = GetUserDivs(tPlayer)
						Tip.Divisions:ClearAllChildren()
						Tip.Text = tPlayer.Name
						for i,v in pairs(Divs) do
						local t = Template:Clone()
							t.Parent = Tip.Divisions
							t.Position = UDim2.new(0, 0, 0, 10 * (i - 1))
							t.Text = v
						if string.find(t.Text, "RAF Police") then
							t.TextColor = Color3.fromRGB(25, 175, 255)
						elseif
							string.find(t.Text, "Royal Air Force") then
								t.TextColor = Color3.fromRGB(10, 110, 255)
						elseif
							string.find(t.Text, "No II") then
								t.TextColor = Color3.fromRGB(50, 100, 225)
						else
							if string.find(t.Text, "RAF Regiment") then
								t.TextColor = Color3.fromRGB(10, 170, 130)
							end
						end
					end
				end
			else
				Tip.Visible = false
		end
	end
end)

I’ve just run the game with a friend and it says I need a BrickColor. Quick unrelated question, why do I need to apply BrickColor instead of Color3 when changing a TextColor value?

Oh my mistake. You need to add 3 to the end t.TextColor. and You’ll be able to use Color3
It needs to look like this:

t.TextColor3 = Color3.new(255,255,255)

Grand, thanks, I’ve also just come across this issue:
https://gyazo.com/c2163b0d855708157251ea5474d8a8c1

any idea why the UI ‘refreshes’?

Also, the colours are wrong because two or more textBoxes contain ‘RAF’, so string.find is becoming confused, any way I can fix that?

It’s refreshing because You update it everytime when moving cursor. To fix the issue you need to add a check if script already shown that player ranks.

And the second issue, maybe try split Group Name and the Group Rank and perform the checks

On the video I didn’t see that group names are getting converted, so try to add Group name conversion in the text label creation too.

local split = string.split(t.Text," - ")
local GroupName = split[1]
if GroupName then
   local Rank = split[2]
   if GroupName == "[RAF] Royal Air Force Police" then
      t.TextColor3 = Color3.new(255,255,255)
      t.Text = "RAF Police - " .. Rank
    --And etc.
   end
end

I’ve fixed the refreshing bit, but the displayed name doesn’t change to the one I tell it to, like I told it to
convert [RAF] Royal Air Force Police to RAF Police but it still shows [RAF] Royal Air Force Police.

Also the colours don’t seem to work either.

Mkay, try this code and let me know if this works:

local CurrentTarget = nil
local Template = script:WaitForChild("Template")

while 1 do
	for _,group in pairs(DivisionList:GetCurrentPage()) do
		do
			Divisions[group.Id] = group
		end
	end
	if DivisionList.IsFinished then
		break
	end
	DivisionList:AdvanceToNextPageAsync()
end
print("got divisions")

GetUserDivs = function(tPlayer) 
	local PlayerGroups = game:service("GroupService"):GetGroupsAsync(tPlayer.userId)
	local ReturnT = {}
	if not Cache[tPlayer.Name] then
		for i,v in pairs(PlayerGroups) do
			if Divisions[v.Id] then
				table.insert(ReturnT,{
					GroupName = v.Name,
					Rank = v.Role
				})
			end
		end
		Cache[tPlayer.Name] = ReturnT
	else
		ReturnT = Cache[tPlayer.Name]
	end
	return ReturnT
end

mouse.Move:connect(function()   
	if mouse.Target then
		local tPlayer = game:service("Players"):GetPlayerFromCharacter(mouse.Target.Parent)
		if tPlayer then
			if CurrentTarget == tPlayer then return end
			Tip.Visible = true
			Tip.Divisions.Visible = true
			CurrentTarget = tPlayer
			local Divs = GetUserDivs(tPlayer)
			Tip.Divisions:ClearAllChildren()
			Tip.Text = tPlayer.Name
			for i,v in pairs(Divs) do
				local t = Template:Clone()
				t.Position = UDim2.new(0, 0, 0, 40 * (i - 1))
				if v.GroupName == "[RAF] Royal Air Force Police" then
					t.Text = "RAF Police - " ..v.Rank
					t.TextColor3 = Color3.fromRGB(25, 175, 255)
				elseif v.GroupName == "RAF || The Royal Air Force" then
					t.Text = "Royal Air Force - " ..v.Rank
					t.TextColor3 = Color3.fromRGB(10, 110, 255)
				elseif v.GroupName == "[RAF] No 1 group" then
					t.Text = "No II - " ..v.Rank
					t.TextColor3 = Color3.fromRGB(50, 100, 225)
				elseif v.GroupName == "[RAF] Royal Air Force Regiment" then
					t.Text = "RAF Regiment - " ..v.Rank
					t.TextColor3 = Color3.fromRGB(10, 170, 130)
				end
				t.Parent = Tip.Divisions
			end
		else
			Tip.Visible = false
			CurrentTarget = nil
		end
	end
end)


while wait() do
	if mouse.Target then
		Tip.Position = UDim2.new(0,mouse.X,0,mouse.Y) + UDim2.new(0,20,0,5)
	end
end

Okay it works well, it’s just there’s a large gap between the groups, which I think is caused because the other groups don’t show up and the space is in a way ‘reserved’ for that certain group name. Is there any fixable way?
https://gyazo.com/3dc3e20c82dcba7664104278ed23e884

Maybe try experimenting with the offset in this line

t.Position = UDim2.new(0, 0, 0, 40 * (i - 1))

Or just make label smaller

Yeah I’ve finished that task. Quick question though, is there any way I can change the offset of the player’s name on the UI?

Script doesn’t touch Player Name Text Label on the UI besides text, so You need to change it directly from the UI

2 Likes