How do I make grid increment smaller?

So I have a system setup to move a model around on a grid type system. I got it to work, but everytime I try to decrease the increment it’ll either not work or it’ll work in 1 position then break when you move the mouse further away.

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local mouse = plr:GetMouse()

RunService.RenderStepped:Connect(function()
	
	local function round(n)
		return math.floor(n + 0.5)
	end
	
	if not game.Workspace:FindFirstChild(plr.Name.."'s Stuff").Temp:FindFirstChild("Object") then
		local object = game.ReplicatedStorage.Things.Objects:FindFirstChild(script.Parent.ObjectType.Value):Clone()
		object.Parent = game.Workspace:FindFirstChild(plr.Name.."'s Stuff").Temp
		object.Name = "Object"
		object:MakeJoints()
		
		
		
		mouse.TargetFilter = object
	end
	
	local part = game.Workspace:FindFirstChild(plr.Name.."'s Stuff").Temp:FindFirstChild("Object")
	part:SetPrimaryPartCFrame(CFrame.new(round(mouse.Hit.p.X), mouse.Hit.p.Y + part.PrimaryPart.Size.X/2, round(mouse.Hit.p.Z)))
end)

If you know a way I can decrease the increment (to 0.25 preferably) then I would appreciate it. As well if you need anymore information just ask.

Thanks,
EvolvedAstro.

1 Like

I solved it by using raycasts instead which is probably for the best.

Here is the important parts of the code for anyone with the same issues.

Grid Function:

local function round(vector,grid) 
	return Vector3.new( 
		math.floor(vector.X/grid+.5)*grid, 
		math.floor(vector.Y/grid+.5)*grid, 
		math.floor(vector.Z/grid+.5)*grid 
	) 
end 

Using Position:

part:SetPrimaryPartCFrame(CFrame.new((round(result.Position + result.Normal * 1.5, 0.5)).X, (round(result.Position + result.Normal * 1.5, 0.5)).Y, (round(result.Position + result.Normal * 1.5, 0.5)).Z))
1 Like

You just need to scale your number before and after you round it so that you can scale the implicit “round to nearest 1” to “round to nearest 0.5”, “round to nearest 0.25”, etc.

function round(n, scale)
   return math.floor(n/scale + 0.5)*scale
end

print(round(0.8, 0.25)) --> 0.75

You got there with your vector3 round function but your implementation is a little odd.