How to make a "grid" that you can place parts on it?

When user clicks the hammer button (right panel) then he would be able to create parts on this grid:

The problem is: how can I make that it can move only in this grid and nowhere else?
Basically my buildings (models) can move only on that grid.

You can see 64 parts with decal on top of each of them.
How can I make a grid for that? How can I place a part on that grid?

I know I have to use UIS to render model in the cursorā€™s position.
If youā€™ve played Lumber Tycoon 2 or any other gamemode with blueprints then youā€™d know what I want to achieve.

1 Like

You could either make the whole game have a grid placing system by looking at the position the player has their mouse at and then floor that location to a grid offset. Or if your looking for aligning to a grid of predefined parts then check what the mouse is hovering over and move the thing you want to place on top.

yeah, can you give me an example of it?
I have no idea how to code it ;/

this tutorial helped me create something similar
https://developer.roblox.com/en-us/onboarding/hit-detection-with-lasers/1

thats not what i want :confused:

I want something like this: Creating A Furniture Placement System
but simpler and working.
I tried cloning the part and setting the position to a cursor butā€¦ how can I make it so its not outside of boundaries?

There is this post:

yeah thatsā€¦ complicated. And nowhere helpful

Iā€™ll give my current script. How can I make it so that its only limited to the plotā€™s boundaries. And can move only every 8 blocks (size of 1 tile in my grid).

local UIS = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local root = script.Parent.Parent.Parent
local topPanel = root.TopPanel

local initSequence = ColorSequence.new({
	ColorSequenceKeypoint.new(0, Color3.fromRGB(0,196,255)),
	ColorSequenceKeypoint.new(1, Color3.fromRGB(255,255,255)),
})

local buildingSequence = ColorSequence.new({
	ColorSequenceKeypoint.new(0, Color3.fromRGB(255,190,0)),
	ColorSequenceKeypoint.new(1, Color3.fromRGB(255,0,0)),
})

local enabled = false

local xAreaConn: RBXScriptConnection
local mouseConn: RBXScriptConnection
local object: Part

local function disconnect()
	xAreaConn:Disconnect()
	mouseConn:Disconnect()
	object:Destroy()
	
	topPanel.Visible = false
	enabled = false

	script.Parent.UIGradient.Color = initSequence
end

script.Parent.MouseButton1Click:Connect(function()
	if enabled then return disconnect() end
	enabled = true
	topPanel.Visible = true

	script.Parent.UIGradient.Color = buildingSequence
	
	object = game.ReplicatedStorage.Part:Clone()
	
	xAreaConn = topPanel.XArea.MouseMoved:Connect(disconnect)
	mouseConn = mouse.Move:Connect(function()
		object.Position = mouse.Hit.Position
		object.Rotation = Vector3.zero
		object.Parent = workspace
	end)
end)

Explorer:
image

Thatā€™s literally what the post I linked talked about and showed (Placing in boundaries and in-game you can see the movement system. If itā€™s too complicated for you and you just wonā€™t try to learn from it I donā€™t know how else to help you

1 Like

The simplest way to tell it is that you need to round the position to the nearest grid tile and then check if thatā€™s inside of the boundaries. If your boundaries arenā€™t aligned to grid, you may need to subtract their position and rotation to get local coordinates to round to.

I recommend just using the post @dummycide linked. You can use it straight up or learn from it to make your own.

u can make it so it checks if its outside certain position in x and z axis and to get it to be a grid use math.floor(number/10+gridsize/10) and times it by 10 later math.floor works by basically taking away the decimal point so for example if the grid is 4 studs per block and mouse is on 4.9 than it would go onto another grid however if it was on 4.5 it would stay at the previous block basically truncation
if you want to learn about math roblox has a developer thing which happens when you search up ā€œroblox mathā€ it could help you with more things so yeah
if ur lazy hereā€™s the link to it:
https://developer.roblox.com/en-us/api-reference/lua-docs/math

theyā€™re using math.clamp to achieve this, with the position, minimum, and maximum as arguments, if you want the exact style they got

Do you want a part placed on randomly on one of those parts?

yes, but they use Knit and other plugins which is making things 100 times more complicated.

Do you want a part placed on randomly on one of those parts?

No. I want to place my part on a part that the cursor points to.
If you move ur cursor to other tile in that grid then the part should ā€œjumpā€ from the last tile to the new one.