Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Any ways to convert a number value to integer [?]

Say that i want to make an altitude for a rocket game. But when I put the position of the rocket, it's a number value (Meaning its not a whole number, it's decimal. Because position property is decimal). So how can I solve this problem? Any ways?

local p = game.Players.LocalPlayer
local n = p.Name.. "'s Folder"

while wait() do
	
	local f = workspace:FindFirstChild(n)
	local part = f.Rocket.Part
	
	script.Parent.Parent.TextLabel.Text = "Altitude: ".. part.Position.Y
	
end

2 answers

✓ Accepted answer

Really, all numbers in Lua are floats. That means you can't "convert" a decimal number to an integer. Sometimes precision errors occur in floats, causing them to add tiny amounts to the actual number. The best way to manage this is by rounding it down with math.floor.

local p = game.Players.LocalPlayer
local n = p.Name.. "'s Folder"

while wait() do

    local f = workspace:FindFirstChild(n)
    local part = f.Rocket.Part

    script.Parent.Parent.TextLabel.Text = "Altitude: ".. tostring(math.floor(part.Position.Y))

end

You can use the new function math.round to quickly round a number:

math.round(part.Position.Y) -- good for visual display

This discussion is preserved as it was. New answers, comments, and votes are closed.