Incorrect imagelabel positioning

Hello, everyone. My problem is pretty simple but I just can not figure it out. I have a health system in my game, and when you pick up a health up, hearts appear in the top left corner. The problem is, that they appear in the wrong order

And here’re the lines that do the things:

	for i = 1, localPlayer.Character:WaitForChild("Humanoid").Health do
		local newHeart = Instance.new("ImageLabel")
		newHeart.Name = "Heart"..i
		newHeart.Parent = script.Parent.Parent.HeartsFolder
		newHeart.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
		newHeart.Size = UDim2.fromScale(0.1, 0.35)
		local XPosition = 0.135 * (i - 1)
		if i > 10 then
			XPosition = 0.135 * (i - math.floor(i/10) * 10)
		end
		newHeart.Position = UDim2.fromScale(XPosition, 0.45 * math.floor((i-1)/10))
		print(newHeart.Name, newHeart.Position)
		
		local newText =  Instance.new("TextLabel")
		newText.Text = i
		newText.Parent = newHeart
		newText.Size = UDim2.fromScale(1, 1)
		newText.Position = UDim2.fromScale(0, 0)
		newText.BackgroundTransparency = 1
		newText.TextScaled = true
	end

Please solve the math for me. Thanks!

Make sure you’re using LayoutOrder and that your UIListLayout or UIGridLayout is set to use it as well.

I’m not using any of those, actually. But it might be a great idea

I tried using them but it was unsuccessful

Oh apologies I skimmed and didn’t notice you using a custom solution! You can actually do this fairly easily by calculating the row and column like so:

local row = i % 10 -- look into how modulus works; super useful
local column = (i-1)/10 // 1

row is a range from 0 to 9 and column will be a range from 1 to n. I noticed you already do the column logic w/ your Y-Axis. Hope this resolves your issue!

Pause I got this backwards LOL

Edited the below, turns out I flip flopped my row and column calcs:

local row = (i - 1) % 10 + 1
local column = math.ceil(i / 10)
1 Like

I was just about to write how your solution was not successful, but now it totally is! The thing I changed was deducting 1 from both equasions, but it’s a personal preference really. Thank you so much!

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