4 stud grid not being aligned properly

  1. What do you want to achieve? Make a 4 stud grid block placement system

  2. What is the issue? The block is not properly aligned on the baseplate.
    I want it to align like this:
    here
    Not like this:
    nothere

I don’t know how to fix this issue. Here’s my code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Part = game.ReplicatedStorage.block
local ts = game:GetService("TweenService")
local partBlacklist = {"SpawnLocation"}

-- functions

local function round(vector,grid) 
	return Vector3.new( 
		math.floor(vector.X/grid+.5)*grid, 
		math.floor(vector.Y/grid+.5)*grid, 
		math.floor(vector.Z/grid+.5)*grid)
end

function tweenpos(part: Instance, x: CFrame.X, y: CFrame.Y, z: CFrame.Z)
	local tween = ts:Create(part, TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {CFrame = CFrame.new(x, y, z)})
	tween:Play()
end

local function Move()
	Part.Parent = workspace
	mouse.TargetFilter = Part
	local pos = round(mouse.Hit.p, 4)
	tweenpos(Part, pos.X, math.round(mouse.Hit.p.Y + 2), pos.Z)
end

-- events

script.Parent.Activated:Connect(function ()
	if mouse.Target == nil then return end
	
	if mouse.Target.Parent.Parent == workspace.spawn or table.find(partBlacklist, mouse.Target.Name) ~= nil then 
		local c = game.ReplicatedStorage.TextLabel:Clone() 
		c.Parent = player.PlayerGui.ScreenGui 
		player.PlayerGui.ScreenGui.error:Play() 
		wait(3) 
		c:Destroy() 
		return 
	end
	
	local pos = Part.Position
	local cf = CFrame.new(pos.X, pos.Y, pos.Z)
	game.ReplicatedStorage.placeblock:FireServer(player, cf, game.JobId)
end)

script.Parent.Equipped:Connect(function ()
	repeat
		wait()
		Move()
	until script.Parent.Parent == player.Backpack
	Part.Parent = game.ReplicatedStorage
end)

script.Parent.Unequipped:Connect(function ()
	Part.Parent = game.ReplicatedStorage
end)
1 Like

I think i got it, the grid system is properly doing its work, however i think the texture on the baseplate is the one that is misaligned.

To fix this you can create an offset in your code:

local Offset = 2 -- im not sure if two is correct, adjust this until it is right
return Vector3.new( 
		math.floor(vector.X/grid+.5)*grid + Offset,
		math.floor(vector.Y/grid+.5)*grid + Offset,
		math.floor(vector.Z/grid+.5)*grid + Offset)

If 2 doesnt really match try -2 or any different number

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