Block placement system

I’m wondering how to create a block placement system. I want the block to align with 4 studs.
Like this:
image

image
I don’t want a whole script, I do want a method or an idea for it.

I’m quite new to the forum so please tell me if this is in the wrong category.

1 Like

Alright, i’ve been experimenting with placement systems in general for months now and they’re pretty easy when u get used to them, first of all u would have to get the Player Mouse by doing player:GetMouse() to find the mouse location obviously, and u would have to use RunService to keep updating the Block Position right? so a quick recapitulation of what we just said.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")
local DummyBlock = game.ReplicatedStorage.Block:Clone(); DummyBlock.Parent = workspace; DummyBlock.Transparency = 0.5; DummyBlock.CanCollide = false

local function roundToStep(initialValue,step)
local roundedValue = math.floor(initialValue/step)*step
return roundedValue --Returns 60
end

RunService.RenderStepped:Connect(function()
    DummyBlock.Position = Vector3.new(roundToStep(Mouse.Hit.Position.X, 4), 0, roundToStep(Mouse.Hit.Position.Z, 4))
end)

Mouse.Button1Down:Connect(function()
    local Block = game.ReplicatedStorage.Block:Clone(); Block.Parent = workspace
    Block.Position = Vector3.new(roundToStep(Mouse.Hit.Position.X, 4), 0, roundToStep(Mouse.Hit.Position.Z, 4))
end)


@xZylter just gave the method and here’s how u could implement it into the script i just gave this would snap the grid to 4 each time u move your mouse,

Although @xZylter i changed math.round to math.floor cause it bugs out when u snap it to 2 different values. Snapping it to math.floor would be a better choice but both still work.

11 Likes

Yep here is that other post with the rounding function to position it according to grid increments for a Vector3.

2 Likes

Expanding off of @ReplicaService’s code to change the step to round to you can use this function.

local function roundToStep(initialValue,step) --Let initialValue = 59, step = 10

    local roundedValue = math.round(initialValue/step)*step

return roundedValue --Returns 60
end
4 Likes
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local RunService = game:GetService("RunService")
local DummyBlock = game.ReplicatedStorage.Block:Clone(); DummyBlock.Parent = workspace; DummyBlock.Transparency = 0.5; DummyBlock.CanCollide = false

local function roundToStep(initialValue,step)
local roundedValue = math.floor(initialValue/step)*step
return roundedValue --Returns 60
end

RunService.RenderStepped:Connect(function()
    DummyBlock.Position = Vector3.new(roundToStep(Mouse.Hit.Position.X, 4), 0, roundToStep(Mouse.Hit.Position.Z, 4))
end)

Mouse.Button1Down:Connect(function()
    local Block = game.ReplicatedStorage.Block:Clone(); Block.Parent = workspace
    Block.Position = Vector3.new(roundToStep(Mouse.Hit.Position.X, 4), 0, roundToStep(Mouse.Hit.Position.Z, 4))
end)


@xZylter just gave the method and here’s how u could implement it into the script, this would snap the grid to 4 each time u move your mouse,

Although @xZylter i changed math.round to math.floor cause it bugs out when u snap it to 2 different values. Snapping it to math.floor would be a better choice but both still work.

4 Likes

You can use the player’s mouse to do this, use the Mouse.Move event to detect if the player’s mouse has moved and the Mouse.Button1Down event to detect if the player has left clicked.

local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()

local block = Instance.new("Part")
block.Size = Vector3.new(4, 4, 4)
block.CanCollide = false
block.Transparency = .5
block.Parent = workspace

local heightOffset = Vector3.new(0, block.Size.Y / 2, 0)

mouse.Move:Connect(function()
    block.Position = mouse.Hit.Position + heightOffset
end)

mouse.Button1Down:Connect(function()
    local placedBlock = block:Clone()
    placedBlock.Transparency = 0
    placedBlock.CanCollide = true
end)
4 Likes

You can add the half of the block size instead of the 0 on the y axis as @aguythatsreallybord said.

Oh yea i already did that lol, thats why i deleted my comment.

1 Like

Hey quick question. In order to make it snap to 3 studs instead of 4 would I just replace all the 4s with 3s? I did that but the block was half in the floor. and it kind of bugging out. Sorry for the inconvenience.

1 Like

Thanks man very useful, is there a way to make a visible grid.