Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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
Ad
Log in to vote
1
Answered by
megukoo 877 Moderation Voter
4 years ago

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

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

Answer this question