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

How would I make a 5x5 Grid placement?

I've been trying to make a Grid placement script, this is what I have:

Player = game.Players.LocalPlayer
Part = game.Workspace.TestPart
Mouse = Player:GetMouse()

MouseDown = false

function SnapToGrid()
	return CFrame.new(
		math.floor(Mouse.Hit.X * 0.5 + 0.5) * 2,
		math.floor(Part.CFrame.Y),
		math.floor(Mouse.Hit.Z * 0.5 + 0.5) * 2
	)
end

Mouse.Button1Down:connect(function()
	if Mouse.Target.Name == "TestPart" then
		MouseDown = true
		repeat
			wait()
			Part.CFrame = SnapToGrid()
		until MouseDown == false
	end
end)

Mouse.Button1Up:connect(function()
	MouseDown = false
end)

But it doesn't exactly work... I've been trying to mess around with the numbers, but it doesn't seem to fix it. Anyone know what i'm doing wrong?

1 answer

✓ Accepted answer

Hello there! I also once had this question, and my friend had to tell me the logic: Imagine that your cursor is on 2.62 and you have a grid of 5.

Let's divide it by your grid size (5): 0.524 Now, round that number. We'll get 1. Do the rounded number times the grid size and TADAA! This will give 5.

Let's try it with another random number. 634.245

634.245 / 5 = 126.849

126.849 rounded = 127

127 * 5 = 635. Boom :)

I don't think I have to put this in a script for you, since you look pretty experienced. However, if you would like me to, feel free to leave a reaction to this answer!

Enjoy and good luck, Jonas

P.s., don't use math.floor only for the rounding! Also use math.ceil, and let the script check which one is the closest to the mouse position. (If on 0.5, it has to be rounded to 1!)

2 comments

0

Thank you it works but it doesn't correctly align with the grid. Would I need to move my grid? Or is there a different way to align it?

0

I think that you mean that the outer borders do not align with it? I fixed that by taking the Y size (Just manually putting an intvalue in every model, and then you can do like (2.625 / 5 ) ==> 1*5 =5 + (The value / 2) I'm nto sure if that would work, since I can't see what you mean with "Doesn't correctly align"

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