Need help on block placement tool

I need help of making a block placement tool. The issue I have is that the block doesn’t go where I click. I’ve tried looking at numerous topics about this and I can’t find any solution.

Client script

–local mouse = player:GetMouse()
local RS = game:GetService(‘ReplicatedStorage’)
local tool = script.Parent
local Event = RS:WaitForChild(‘PlaceBlock’)
local pos

local block = game.ReplicatedStorage.Block

script.Parent.Activated:Connect(function()
local target = mouse.Target
if target then
print(target.Position)
if (target.Position - player.Character.HumanoidRootPart.Position).magnitude <= 12 then
local pos = target
Event:FireServer(target,pos,block)
end
end
end)

server script

–local RS = game:GetService(‘ReplicatedStorage’)
local Event = RS:WaitForChild(‘PlaceBlock’)

Event.OnServerEvent:Connect(function(player,target,pos,Block)
local clonedBlock = Block:Clone()
clonedBlock.Position = target.Position
clonedBlock.Parent = game.Workspace
end)

1 Like

Can you post a screenshot of how it goes wrong?

One thing that might cause it is this:

clonedBlock.Position = target.Position

IIRC, setting the Position of a Part causes it to appear on top of any other Parts that are in the way. Try this instead:

clonedBlock.CFrame = CFrame.new(target.Position)

When I click, the blocks doesn’t go where it has to go.
Screenshot 2020-11-28 043344

Hi!

Have you tried to shoot a Ray into the direction the mouse was pointed in at the time of being clicked and then take the position where it first touched another part to put the part?

local unitRay = camera:ScreenPointToRay(mouse.Position.X, mouse.Position.Y)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)

cube.CFrame = CFrame.new(ray.Origin, ray.Origin + ray.Direction)