How do I make a grid of a wall on X axis?

This is what I’ve done so far, placing a wall at the border of one tile in the Z axis:


the code:
`
local RUNSERVICE = game:GetService(“RunService”)

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

local grid = 8

RUNSERVICE.Heartbeat:Connect(function(deltatime)
local mousepos = mouse.Hit.Position

local localPos = game.Workspace.Center.CFrame:ToObjectSpace(CFrame.new(mousepos))

local gridV3 = Vector3.new(
	math.round(localPos.X/grid) * grid,
	0,
	math.round(localPos.Z/grid) * grid
)

local worldPos = game.Workspace.Center.CFrame:ToWorldSpace(CFrame.new(gridV3))

game.Workspace.Wall:PivotTo(worldPos)

end)
`

It works just fine, but what I want is to make the grid now for X axis,

I want to turn 90 the grid and make the wall to be on the left or right side depending on which one is closer.

I can’t think of how to do it, how could I?

4 Likes
--LocalScript in StarterPlayerScripts
local rs = game:GetService("RunService")
local center = workspace:WaitForChild("Center")
local wall = workspace:WaitForChild("Wall")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local grid = 8

rs.Stepped:Connect(function()
	local mousePos = mouse.Hit.Position
	local localPos = center.CFrame:PointToObjectSpace(mousePos)
	local gx = math.round(localPos.X / grid) * grid
	local gz = math.round(localPos.Z / grid) * grid
	local left = Vector3.new(gx - grid/2, 0, gz)
	local right = Vector3.new(gx + grid/2, 0, gz)
	local distLeft = (localPos - left).Magnitude
	local distRight = (localPos - right).Magnitude
	local chosen = distLeft < distRight and left or right
	local worldPos = center.CFrame:PointToWorldSpace(chosen)
	wall:PivotTo(CFrame.new(worldPos) * CFrame.Angles(0, math.rad(90), 0))
end)

You can use the </> option when posting scripts, to put them in these code boxes.
Thank you for not using a picture.

1 Like
local RUNSERVICE = game:GetService(“RunService”)

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

local grid = 8

RUNSERVICE.Heartbeat:Connect(function(deltatime)
local mousepos = mouse.Hit.Position

local localPos = game.Workspace.Center.CFrame:ToObjectSpace(CFrame.new(mousepos))

local gridV3 = Vector3.new(
	math.round(localPos.X/grid) * grid,
	0,
	math.round(localPos.Z/grid) * grid
)

local worldPos = game.Workspace.Center.CFrame:ToWorldSpace(CFrame.new(gridV3))

game.Workspace.Wall:PivotTo(worldPos)

end)

in your code u use worldpos as a Cframe
CFrame * Cframe.orientation might help you out

but keep in mind after rotating it 90 degrees you alsow need to adjust the location

Thank you, i did “</>” but for some reason it just did some part of the text code.

1 Like

A rem can do that if it has a … in it. Or it runs into an odd … in the script.

Keep in mind your object walls need to be twice as long as your grid size to line up at the corners if one is rotated (making a corner). If your walls are 8 studs long, your grid should be 4.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.