Bigger/smaller than operators not working correctly

Hi,
I’m creating a strategy game where you can create your own maps. However, when I tested out the background editor, where you can choose the x to z ratio of the background, I came across a problem. The ‘>’ operator doesn’t seem to be working correctly. It (most likely) only looks to the first character and (probably) only looks to the second character if the first two are equal (e.g. in 3:20 it compares 3 and 2, and in 2:20 it compares 2 and 20)

image

image

The relevant code:

Remotes.MapChangeEvent.OnServerEvent:Connect(function(plr, ID, ratio_array)
	backgr.MapImage.Texture = getImageIdFromDecal(ID)
	print(ratio_array[1]+2) -- the +2 here is to show that the value is a number, not a string
	print(ratio_array[2]+2)
	if ratio_array[1] > ratio_array[2] then
		print(tostring(ratio_array[1]).." is bigger than "..tostring(ratio_array[2]))
		backgr.Size = Vector3.new(2048, 0.1, ratio_array[2]/ratio_array[1]*2048)
	else
		print(tostring(ratio_array[1]).." is smaller than "..tostring(ratio_array[2]))
		print(ratio_array)
		backgr.Size = Vector3.new(ratio_array[1]/ratio_array[2]*2048,0.1,2048)
	end
end)

I have never seen this behavior before, but maybe I missed something?
If you need extra code, please let me know!

2 Likes

ratio_array most likely contains strings and not numbers, so the result comes from the comparison of individual chars. Convert both sides to numbers first.

The print statements work fine because Luau attempts to coerce types to change.

print(1 + "2") --> 3 (number)
local a = "2"
local b = "12"
if a > b then
    print("a") -- prints (first char compared)
else
    print("b")
end
1 Like

Yep, that was the problem…
I had no idea that was even a feature, but thank you.

That’s such a stupid reason to make that error :man_facepalming:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.