Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I fix my bricks getting into each other when you place them at certain angles?

I have a 4x4 grid building script. Whenever i try to place a block by the side of another block, the block instead just goes into the other block.

I recommend testing this and building with it for a while to understand what I am talking about.

db = true


local tool = script.Parent

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


tool.Activated:connect(function()
    if db then
        db = false
        print("placed brick")
        local part = game.ReplicatedStorage.Dirt:Clone()
        part.Parent = workspace
        local offsety = 2
        local a = mouse.Hit -- point a
        local x = a.X
        local y = a.Y
        local z = a.Z
        local x = math.floor(x / 4) * 4
        local y = math.floor(y / 4) * 4
        local z = math.floor(z / 4) * 4
        c = CFrame.new(x,y,z) + Vector3.new(2,offsety,2)
        print(c)
        part.CFrame = c
        wait(.2)
        db = true
    end
end)

"Dirt" is a 4x4 part. db is the cooldown for placing blocks

edit: here's a demonstration of what i am talking about https://youtu.be/sGb2sb4mclo

1 answer

Use mouse.Target to get the position of the object that the mouse is touching. This way, if you touch the Dirt object, you will get that position and you won't have to do the xyz calculations.

-- use variables!
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local dirt = ReplicatedStorage:WaitForChild("Dirt")
local offsetVector = Vector3.new(2, 2, 2)

tool.Activated:Connect(function() -- use Connect instead of connect
	if not db then return end
	db = false
	local mouseTarget = mouse.Target
	if not mouseTarget then return end
	
	local part = dirt:Clone()
	part.CFrame = mouseTarget.CFrame + offsetVector
	part.Parent = workspace -- parent the part last!
	wait(.2)
	db = true
end)

Hope this helps! :)

7 comments

0

this is very odd, but upon adding my variables and testing the script i get this error 15:58:30.344 - Players.bartekrabit.Backpack.Dirt.Script:22: bad argument #1 to 'new' (Vector3 expected, got CFrame) (line 13 in your script) it's odd because it's cframe.new not vector3.new

0

You can't pass a CFrame to CFrame.new(). Just remove the outter CFrame.new() part and leave the 'mouseTarget.CFrame' portion.

0

there's a big issue with this script. it only gets the CFrame of the part and places a block 2,2,2 away

0

Sorry, I made the script in a hurry. @ScrewDeath you're right about that, fixed it.

0

@bartekrabit I'm not sure what your goal is, which is why that may occur. So you are trying to place a new block (2, 2, 2) away from the dirt?

0

try the script yourself and you'll see excatly what is my idea.

0

Ah okay. So I tried out the script, but I don't see a problem with it: https://i.gyazo.com/58bacc5e04473758dba6ed6d8eeb24a4.mp4

This discussion is preserved as it was. New answers, comments, and votes are closed.