Help with grid building system

With my script i cannot place the block on the bottom, left and front of the block if i aim it there it will just go inside the block im aiming at
Screen Shot 2024-07-11 at 22.50.13

This is my code:

local gui = script.Parent
local template = gui.Frame.Area.Template

function round(num, mult)
	return math.round(num / mult) * mult
end

function roundToGrid(pos)
	local gridPos = Vector3.new(round(pos.X, 2), round(pos.Y, 2), round(pos.Z, 2))

	return gridPos
end


for i, v in pairs(game.ReplicatedStorage.Objects:GetChildren()) do
	local newButton = template:Clone()
	newButton.Parent = gui.Frame.Area
	newButton.Visible = true

	local displayObject = v:Clone()
	displayObject.Parent = newButton.ViewportFrame

	newButton.MouseEnter:Connect(function()
		game:GetService("TweenService"):Create(newButton, TweenInfo.new(0.1), {Size = UDim2.new(newButton.Size.X.Scale * 1.2, newButton.Size.X.Offset * 1.2, newButton.Size.Y.Scale * 1.2, newButton.Size.Y.Offset * 1.2)}):Play()
	end)

	newButton.MouseLeave:Connect(function()	
		game:GetService("TweenService"):Create(newButton, TweenInfo.new(0.1), {Size = template.Size}):Play()
	end)

	newButton.MouseButton1Click:Connect(function()
		local mouse = game.Players.LocalPlayer:GetMouse()
		local clicked = false
		local newObject = v:Clone()
		newObject.Parent = workspace.TemporaryParts
		newObject.Transparency = 0.5
		newObject.Anchored = true
		newObject.CanCollide = false

		mouse.Button1Down:Connect(function()
		if clicked == false then
			clicked = true
			game.ReplicatedStorage.Events.Place:FireServer(v, newObject.Position)
			newObject:Destroy()
		end
		end)

		while clicked ~= true do
			local cursorPos = mouse.Hit.Position
			mouse.TargetFilter = workspace.TemporaryParts
			if mouse.Target then
				newObject.Position = roundToGrid(cursorPos)
				print(roundToGrid(cursorPos))
			end
			wait()
		end
	end)
end

Sorry if i didnt explain it well i don’t know how to explain my problem

I have experienced this before, and the fix is pretty simple. You can get the face clicked on by Mouse.TargetSurface, and check if it is any of those 3, in which case, subtract a single axis Vector3 multiplied by the grid size, for Front, it should be Vector3.zAxis * gridSize, for Bottom it should be Vector3.yAxis * gridSize and for Left it should be Vector3.xAxis * gridSize. Simply subtract these from the position, and done.

thanks it works (30 chaaaaars)

sorry it doesnt work actually the surfaces varies it depends on the places the blocks are im so confused

I found this post and it works

1 Like

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