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)
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)
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.
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.)
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)