Help with grid script

I’m trying to make a game where you can create blocks at the players position, but the script doesn’t work, and there aren’t any warnings

heres the code

local FloorPart = game.ReplicatedStorage.Floor

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	print("found player")
	local New = FloorPart:Clone()
	print("cloned")
	New.Position = Vector3.new(math.floor(player.Character.HumanoidRootPart.Position.X),.5,math.floor(player.Character.HumanoidRootPart.Position.Z))
	print("changed position")
	New.Parent = workspace
	print("changed parent")
end)

I don’t know how I am meant to do this, because the first programming language I actually learnt was python, so I’m just used to using

x = 10.0348503
round(x)
print(x)

and the out put would just be 10, but I have no clue how to do this is in LUA,

thanks in advance

In lua there are 2 rounding methods in the math library:
math.floor(x) – Rounds the number x down to the nearest integer
math.ceil(x) – Rounds the number x up to the nearest integer

Note, this does not change the variable x to the rounded number, if you wish to do this you need a separate variable e.g.

local x = 10.0348503
local down = math.floor(x) -- 10
local up = math.ceil(x) -- 11

Another neat trick you can use for rounding numbers is through formatted ASCII text like this:

local rounded = tonumber(string.format("%.1f", 10.0348503)) -- 10

The .1 is how many decimal places you want to go. So you could change it to 0.2 for 2 d.p.

2 Likes

I previously shared my snapping function which takes two arguments a number and an increment here:

1 Like