If and elseif statements not working at all?

This issue has been occurring for ages, and even after my PC got reset a couple days ago this issue still happening and I don’t understand why.

In this screenshot you can clearly see that it prints 0.8
image
Yet it won’t change the boxs image from gray to orange:
image
Its supposed to look like this:
image

script.Parent:GetPropertyChangedSignal("BackgroundColor3"):Connect(function()
	local Color = script.Parent.BackgroundColor3.R
	local Sub = string.sub(Color, 0, 3)
	print(Sub)
	if Sub == 0.1 then
		script.Parent.Background.Image = "rbxassetid://4589621877"
	elseif Sub == 0.8 then
		script.Parent.Background.Image = "rbxassetid://4589629895"
	elseif Sub == 0.6 then
		script.Parent.Background.Image = "rbxassetid://4589635539"
	end
end)
1 Like

Have you tried using tonumber? string.sub returns another string, and a string does not equal a number.
Instead of

local Sub = string.sub(Color, 0, 3)

Maybe try

local Sub = tonumber(string.sub(Color, 0, 3));
1 Like

@aIphabox
To add onto what @Nucl3arPlays is saying I would like to note you may run into a similar problem in the future if you happen to expound onto a similar idea.

String’s have coercion in Lua (5.1); this means when you combine some of the mathematical operators with them they will change from a string datatype to a number datatype. This can be very confusing and can lead to misleading information. (Perhaps as things such as "1" == 1 returning true)

"1" + 1 --// yields 2
"10" / "2" --// yields 5
"6"^2 --// yields 36

However:
"10" == 10 --// yields FALSE not true; the equivalence operator does not support the coercion

Further Reading:

From search Color3.r returns a number from the Red part of the colour
so any string process wont work.
I believe the number returned would be between 0 and 1 so the if statements need to be like if Color == 0.1 etc

then try first with only one of the elseif statement. i mean, first try maybe only (you can try to leave the third parameter in the string.sub function):

script.Parent.BackgroundColor3.r = 0.8
wait(2.5)
script.Parent:GetPropertyChangedSignal("BackgroundColor3"):Connect(function()
local Color = script.Parent.BackgroundColor3.R
local Sub = string.sub(Color, 0)
print(Sub)
    if Sub == 0.8 then
	script.Parent.Background.Image = "rbxassetid://4589629895"
end
end)

If this script dont work then you false compare the two values and schould use the tonumber function like @Nucl3arPlays have say it, else if this work then i dont know you problem is.