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 placement grid?

I'm making a build tool that creates parts wherever you click in the world. Thes script makes the parts do not pass trough each other, but, sometimes,if you put a part between two parts that are so together the part will pass through the other parts and i'd like to know how to make a placement grid to make the parts line up.

Here's a picture to help you understand my problem.

https://ibb.co/jb6KaU

Here's the scripts.

-- Local script in the tool 

local Event = game:GetService("ReplicatedStorage"):WaitForChild("PartsFromTool")
local equipped = false

script.Parent.Equipped:Connect(function(mouse)
	
	equipped = true

	game:GetService("UserInputService").InputBegan:Connect(function(input, g)
	
		if (equipped == true) then
	
			if (input.UserInputType == Enum.UserInputType.MouseButton1) then
				
				local position = mouse.Hit
				
				Event:FireServer(position)
		
			end
	
		end
	
	end)
	
end)

script.Parent.Unequipped:Connect(function()
	
	equipped = false
	
end)	

-- Server Script in ServerScriptService

game:GetService("ReplicatedStorage"):WaitForChild("PartsFromTool").OnServerEvent:Connect(function(player, position)
	
	local PCreated = Instance.new("Part")
	PCreated.Parent = game.Workspace
	PCreated.Size = Vector3.new(3, 3, 3)
	PCreated.Material = "Brick"
	PCreated.BrickColor = BrickColor.new("Persimmon")
	PCreated.CFrame = CFrame.new(position.Position) * CFrame.Angles(0, 0, 0)
	wait(0.001)
	PCreated.Anchored = true
	PCreated.CFrame = CFrame.new(PCreated.Position) * CFrame.Angles(0, 0, 0)
	
end)

4 comments

0

So you want to snap the parts to a grid so you can't place a part inside another part? Also the link just brings me to a blank page.

0

Yes, i want that, and here's another link: https://image.ibb.co/mJ8XvU/Captura.png

0

I have found something that really helped me, make sure to check this! "https://web.roblox.com/games/1095570774/Placement-System-Example"

0

Answered!

1 answer

✓ Accepted answer

To snap to a grid you need to do a bit of math. First you divide the mouse position by your grid then add 0.5 next round down or up and finally multiply by your grid. This will center the model on a grid of your choice. For rounding I will use math.floor() this rounds down no matter the value unless it is already an int (whole number).

Here is the code for snapping to a grid:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local Part = Instance.new("Part")
Part.Anchored = true
Part.Parent = workspace

local PosX, PosY, PosZ
local Grid = 2 -- Change this to your grid size

local function Snap()
	PosX = math.floor(mouse.Hit.X / Grid + 0.5) * Grid -- Snaps
	PosY = 1 -- Change this to your Y 
	PosZ = math.floor(mouse.Hit.Z / Grid + 0.5) * Grid -- Snaps
end

local function Move()
	mouse.TargetFilter = Part -- Ignores the part

	Snap()

	Part.Position = Vector3.new(PosX, PosY, PosZ) -- Sets the position
end

Mouse.Move:Connect(Move)

Now I used Vector3.new() but you will want to use CFrame's.

Hope this works!

9 comments

0

The "Grid" is supposed to be the quantity of Studs?

0

And i have to create the part from a Server script because of the Filtering Enabled. I'll do that change, i think it works nice.

0

I made some changes and it worked perfect! Thanks for your help.

0

Hey, i found a problem and i hope you can help me again. The problem is: I can place a part above other part. Is there any way to fix it? I tried doing Y = math.floor(mouse.Hit.Y / grid + 0.5) * grid, but the part starts moving everywhere!

0

I can't*

0

Don't do the Y position on a grid

0

But if i do that: Y = 1 or Y = 2, i will just can place blocks in that Y position, and not place blocks above or below another blocks.

0

And is there any difference if i use math.ceil() instead f math.floor()?

0

math.ceil() rounds up and math.floor() rounds down

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