Help with math.round

So, math.round is not working. I am trying to round (1*5/5), and it should be 1, but it doesn’t show instead and it becomes 0.

for i, item in next, stockItems:GetChildren() do
	if item:IsA("Folder") then
		local clonedTemplate = template:Clone()
		clonedTemplate.Parent = scrollingFrame
		clonedTemplate.Name = item.Name
		clonedTemplate.Visible = true
		clonedTemplate.Text = item.Name
		
		clonedTemplate.Activated:Connect(function()
			selectedTemp = clonedTemplate
			local selectedItem = stockItems:FindFirstChild(selectedTemp.Name)
			price.Text = tostring("$"..selectedItem.Price.Value)
			name.Text = selectedItem.Name
		end)
		
		buyButton.Activated:Connect(function()
			if selectedTemp ~= nil then
				local selectedItem = stockItems:FindFirstChild(selectedTemp.Name)
				local itemPrice = selectedItem:WaitForChild("Price")
				
				if player.leaderstats.Coins.Value >= (itemPrice.Value/5) then
					player.leaderstats.Coins.Value -= (itemPrice.Value/5)
					player.ItemsInStock[selectedTemp.Name].Value += math.round(1/5) -- the number 1 is automatically becoming 5 so its essentially 5/5
				end
			end
		end)
	end
end

Example: 
![2022-03-05_13-52-28|video](upload://m3hmC88HMpZbK7SYGFZ8Sg2x8YY.mp4)
1 Like

An easy way of rounding numbers would be this.

local function round(Number)
	local RoundingNum = Number - math.floor(Number)
	if RoundingNum >= 0.5 then
		return math.ceil(Number)
	else
		return math.floor(Number)
	end
end
print(round(1*5/5)

You’re not doing (1*5/5).

1 divided by 5 is .2, math.round will round up if the number is .5 or above and down if its under.
math.round(1*5/5) is 1.

1 Like

Why not just math.floor(Number+0.5)?
round(2.499) → 2
round(2.501) → 3

1 Like

No, I don’t know why but it runs the function 5 times in a row (only for the number 1), so its essentially 1*5/5.

Oh I see. Well thats still your problem.
rounding 1/5 is zero and adding 0 onto anything is just the original number.

1 + 0 = 1
5 + 0 = 5
etc
You’re just adding 0 to the value lol

Well removing math.round may solve your problem but I don’t really know.

If I remove math.round it just becomes a decimal like 1.99998.

1/5 = 0.2
math.round(0.2) = 0

Then there’s literally no point of doing that line?

Also as @anthropomorphic_dev said, you’re not doing (1*5/5) or else it would’ve added it by 1.

This is functioning properly. 1/5 is not equal to 1*5/5. 1/5 = 0.2, and rounding 0.2 is = 0.

The script makes the number times five automatically. I don’t know how to fix it so I just made everything divided by 5, so its essentially 1*5/5 or 5/5.

Ah, I think I know what you are saying. You are saying that:
player.ItemsInStock[selectedTemp.Name].Value is equal to 5?

What you would want to do is replace the line with:

math.round(player.ItemsInStock[selectedTemp.Name].Value + 1/5)

(I think)

What I mean is that the 1 inside of my script is equivalent to 5, so I need to make it 1 so I am dividing it by 5, but it becomes a decimal if I do it more than once, so I am trying to round it up. (Also don’t ask why I don’t just use 1, because my script makes it 5.)

Thank you for your help anyway!

1 Like

The reason why the function repeats 5 times everytime you press the buyButton is because you placed the function inside the loop. Consider putting it outside like this

for i, item in next, stockItems:GetChildren() do
	if item:IsA("Folder") then
		local clonedTemplate = template:Clone()
		clonedTemplate.Parent = scrollingFrame
		clonedTemplate.Name = item.Name
		clonedTemplate.Visible = true
		clonedTemplate.Text = item.Name
		
		clonedTemplate.Activated:Connect(function()
			selectedTemp = clonedTemplate
			local selectedItem = stockItems:FindFirstChild(selectedTemp.Name)
			price.Text = tostring("$"..selectedItem.Price.Value)
			name.Text = selectedItem.Name
		end)
	end
end

buyButton.Activated:Connect(function()
	if selectedTemp ~= nil then
		local selectedItem = stockItems:FindFirstChild(selectedTemp.Name)
		local itemPrice = selectedItem:WaitForChild("Price")
		
		if player.leaderstats.Coins.Value >= itemPrice.Value then
			player.leaderstats.Coins.Value -= itemPrice.Value
			player.ItemsInStock[selectedTemp.Name].Value += 1
		end
	end
end)

1 Like

Thank you so much, I was having so much trouble with this!

1 Like