How would I improve my building/stamper tool?

I have recently been working on a building/stamper tool for a plot building game, and I’ve encountered an issue I’m not sure I can overcome. I am relatively new to making these types of things (building tools), and I’d like for some help & suggestions to be thrown at me so I can overcome these issues and get the ball rolling again.

The issue I’ve been encountering is the building tool not latching onto the blocks in front of you, or if you are on the right side of the block facing in front of it. Forgive my wording of the phenomenon, as it is a bit complicated for me to explain. Anyways, basically if you go on either one of these sides, it’ll just force you to place a new block inside of the pre-existing one.

Here is the LocalScript for it (currently client-sided only):

-- define vars
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local block = game.ReplicatedStorage:WaitForChild("Blocks").Part

local ghostBlock = block:Clone()
ghostBlock.Parent = game.Workspace
ghostBlock.Transparency = .3
ghostBlock.CanCollide = false
ghostBlock.Color = Color3.new(0.309804, 0.529412, 1)

mouse.TargetFilter = ghostBlock

local studs = 4

-- finding block position
mouse.Move:Connect(function()
	local mousePos = mouse.Hit.Position
	local blockPos = Vector3.new(0, block.Size.Y / 2, 0)
	blockPos += mousePos
	
	local xPos = math.round(blockPos.X / studs) * studs
	local yPos = math.round(blockPos.Y / studs) * studs
	local zPos = math.round(blockPos.Z / studs) * studs
	blockPos = Vector3.new(xPos, yPos, zPos)
	
	ghostBlock.Position = blockPos
end)

-- placing blocks
mouse.Button1Down:Connect(function()
	local placedBlock = block:Clone()
	placedBlock.Parent = game.Workspace
	placedBlock.Position = ghostBlock.Position
	placedBlock.CanCollide = true
	
end)

Any help whatsoever would be appreciated!

I recreated your scenario in studio and I managed to fix the issue using Raycasts!

Modified code:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local block = game.ReplicatedStorage:WaitForChild("Blocks").Part

local ghostBlock = block:Clone()
ghostBlock.Parent = game.Workspace
ghostBlock.Transparency = .3
ghostBlock.CanCollide = false
ghostBlock.Color = Color3.new(0.309804, 0.529412, 1)

mouse.TargetFilter = ghostBlock

local studs = 4

-- finding block position
mouse.Move:Connect(function()
	local blockPos = Vector3.new(0, block.Size.Y / 2, 0)
	local mousePos = mouse.Hit.Position;
	local origin = mouse.Origin.Position -- Basically the position of the camera
	local dir = mouse.UnitRay.Direction -- Getting the direction of the mouse
	
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Include
	params.FilterDescendantsInstances = {workspace.Blocks, workspace:WaitForChild("Baseplate")}
	
	local ray = workspace:Raycast(origin,dir*5000,params)
	if ray then -- checking if it hit anything
		blockPos += mousePos + ray.Normal
	else
		return
	end
	
	local xPos = math.round(blockPos.X / studs) * studs
	local yPos = math.floor(blockPos.Y / studs) * studs
	local zPos = math.round(blockPos.Z / studs) * studs
	blockPos = Vector3.new(xPos, yPos, zPos)
	
	ghostBlock.Position = blockPos
end)

-- placing blocks
mouse.Button1Down:Connect(function()
	local placedBlock = block:Clone()
	placedBlock.Parent = workspace.Blocks
	placedBlock.Position = ghostBlock.Position
	placedBlock.CanCollide = true
end)

Sorry for the audio, no idea why it does that

Thank you so much! Now I can see why I should’ve used raycasts, but atleast now I know what the basis for making a building tool is lol

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