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

Keeping a model to a grid?!

Any opinions on how I'd keep a 20x20 model on a 5x5 grid?

I've already tried some other code, but it didn't work, well it did, but it was inaccurate.

My code:

Item:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X - Mouse.Hit.p.X%5, yAxis, Mouse.Hit.p.Z - Mouse.Hit.p.Z%5))	

1 answer

✓ Accepted answer

It's pretty easy, if your question is "How do I snap it to a grid".

local function round(i) --You make a function to round to the nearest number, it would have a single parameter.
	return math.floor(i+.5) --math.floor + .5 will round to the nearest.
end

local function snap(p,gW,gD) --The snap function, it should have three parameters, the position, the Grid width and Grid depth
	return Vector3.new(
		round(p.x / gW)*gW, --Simple equation, shouldn't need to be explained as it is really that simple.
		1, --You could also use the rounded number of p.y /a grid height * a grid height for this.
		round(p.z / gD)*gD --repeat the equation.
		)
end

Next you would write a function that snaps it's position when you want it to.

I.E.

button.MouseButton1Clicked:connect(function()
	game.Workspace.SnapPart.CFrame=snap(game.Workspace.SnapPart.CFrame,5,5)
end)

Sources: Math functions, Using return, Gui click, CFrame, Vector3

2 comments

0

How will I perhaps move the model on a 5x5 grid? Like, make it move every 5 studs?

0

Pass the mouse.hit.p through the snap function and set the brick's CFrame to what it returns, for your specific problem.

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