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

Why is this script not changing the colors of the text in a GUI?

The script is supposed to change the text and the color of the text when values change. The script only changes what the texts says, and not its color. Output did not give me anything related to the script.

Can someone please help me?

Here's the script:

local score = game.ServerScriptService.MainGame:WaitForChild("score1")
local score2 = game.ServerScriptService.MainGame:WaitForChild("score2")

while wait(0.1) do
	if score2.Value > score1.Value then
		wait(0.1)
		script.Parent.TextColor3 = Color3.new(72,255,46)
		wait(0.1)
		script.Parent.Text = "Winning"
	elseif score2.Value < score1.Value then
		wait(0.1)
		script.Parent.TextColor3 = Color3.new(255,58,65)
		wait(0.1)
		script.Parent.Text = "Losing"
	elseif score2.Value == score1.Value then
		wait(0.1)
		script.Parent.TextColor3 = Color3.new(124,156,255)
		wait(0.1)
		script.Parent.Text = "Tied"
	end
end

1 answer

✓ Accepted answer

Color3's work on a scale of numbers between 0-1. Since most people like to use a scale of 0-255, we need to divide every number by 255 to make it fit into the Color3's requirements.

So, let's make ourselves a function that will return a Color3 with each number divided by 255.

function ChangeColor3(r,g,b)
	return Color3.new(r/255,g/255,b/255)
end

Now, we can apply the function like so:

script.Parent.TextColor3 = Change(255,58,65) --Since the ChangeColor3 function returns a Color3, the TextColor3 will be the returned value from the function.

Finally, let's put it all together! Also, you don't need to wait while changing each property, just do it instantly and it'll work fine.

local score = game.ServerScriptService.MainGame:WaitForChild("score1")
local score2 = game.ServerScriptService.MainGame:WaitForChild("score2")

function ChangeColor3(r,g,b)
	return Color3.new(r/255,g/255,b/255)
end

while wait() do
    if score2.Value > score1.Value then
        script.Parent.TextColor3 = ChangeColor3(72,255,46)
        script.Parent.Text = "Winning"
    elseif score2.Value < score1.Value then
        script.Parent.TextColor3 = ChangeColor3(255,58,65)
        script.Parent.Text = "Losing"
    elseif score2.Value == score1.Value then
        script.Parent.TextColor3 = ChangeColor3(124,156,255)
        script.Parent.Text = "Tied"
    end
end

We can improve the script by making the code a function and then connecting it to the Changed function of each score value.


Final code:

local score = game.ServerScriptService.MainGame:WaitForChild("score1")
local score2 = game.ServerScriptService.MainGame:WaitForChild("score2")

function ChangeColor3(r,g,b)
	return Color3.new(r/255,g/255,b/255)
end

function updateScore() --updateScore is a variable we can use to call the function.
    if score2.Value > score1.Value then
        script.Parent.TextColor3 = ChangeColor3(72,255,46)
        script.Parent.Text = "Winning"
    elseif score2.Value < score1.Value then
        script.Parent.TextColor3 = ChangeColor3(255,58,65)
        script.Parent.Text = "Losing"
    elseif score2.Value == score1.Value then
        script.Parent.TextColor3 = ChangeColor3(124,156,255)
        script.Parent.Text = "Tied"
    end
end

score.Changed:connect(updateScore) --Connects the score's Changed event to the updateScore function
score2.Changed:connect(updateScore)

I hope my answer helped you. If it did, be sure to accept it.

3 comments

0

I advise against calling your function Color3. This will cover up the actual Color3, and make it so that code wanting to use the normalColor3.new constructor won't be able to. I suggest using Color instead. (Also your code as written won't work because the global variable will overshadow the Color3 anyway)

0

That makes sense. I'll change this now.

0

Thanks!

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