Grid building system

I am working on a grid placement. The only thing that I am struggling with is the grid part. I tried making a grid script using a video but it doesn’t work. I keep getting errors say that the brick’s parent is locked. I am trying to make a building system like in the game “skywars”. Edit: I forgot that when I equip the tool, the block doesn’t show up

picture of what I am talking about:

Here’s the code I have.

local player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
local off = false
local posx
local posy
local posz

local gridsize = 4

local clonedbrick = game.ReplicatedStorage.Brick:Clone()

local function snap(brick)
	posx = math.floor(mouse.Hit.X/gridsize + 0.5) * gridsize
	posy = clonedbrick.Position.Y
	posz = math.floor(mouse.Hit.Z/gridsize + 0.5) * gridsize
end


script.Parent.Equipped:Connect(function()
	if mouse ~= nil then
		mouse.TargetFilter = clonedbrick
		snap()
		clonedbrick.Transparency = .5
		clonedbrick.Parent = workspace
		while mouse ~= nil do
			clonedbrick.Position = mouse.Hit.p + Vector3.new(posx,posy,posz)
			wait()
			if off then
				clonedbrick:Destroy()
				off = false
				break
			end
		end
	end
end)
script.Parent.Activated:Connect(function()
	local BrickToPlace = game.ReplicatedStorage.Brick:Clone()
	local pos = mouse.Hit.p + Vector3.new(posx,posy,posz)
	if(pos - player.Character.HumanoidRootPart.Position).magnitude <= 12 then
		script.Parent.RemoteEvent:FireServer(pos)
	end
end)
script.Parent.Unequipped:Connect(function()
	off = true
end)

Thank you, any help will be appreciated.

1 Like

clonedbrick’s parent is locked because you are Destroying it. That’s what Destroy does - locks the parent.

Make a new clone every time, instead of trying to reuse the same one.

Alternatively, use clonedbrick.Parent = nil instead of clonedbrick:Destroy(), but it feels cleaner to me to just make a new clone.

Thank you for the help, now I don’t have an error. I accidentally forgot to put that the block does not show up when it is supposed to.

1 Like

Here’s a video robloxapp-20201129-1220277.wmv (1.5 MB)

1 Like

Here’s the script I have made, but there are still problems like when I unequip the tool, the block doesn’t go away. Does anybody know how to fix this?

local player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
local off = false
local posx
local posy
local posz

local gridsize = 4

local function snap(clonedbrick)
	posx = math.floor(mouse.Hit.X/gridsize + 0.5) * gridsize
	posy = math.floor(mouse.Hit.Y/gridsize + 0.5) * gridsize
	posz = math.floor(mouse.Hit.Z/gridsize + 0.5) * gridsize
end


script.Parent.Equipped:Connect(function()
	local clonedbrick = game.ReplicatedStorage.Brick:Clone()
	mouse.TargetFilter = clonedbrick
	mouse.Move:Connect(function()
		snap()
		clonedbrick.Transparency = .5
		clonedbrick.Parent = workspace
		while mouse ~= nil do
			clonedbrick.CFrame = CFrame.new(posx,posy,posz)
			wait()
			if off == true then
				clonedbrick.Parent = nil
				off = false
				break
			end
		end
	end)
end)
script.Parent.Activated:Connect(function()
	local BrickToPlace = game.ReplicatedStorage.Brick:Clone()
	local pos = mouse.Hit.p + Vector3.new(posx,posy,posz)
	if(pos - player.Character.HumanoidRootPart.Position).magnitude <= 12 then
		script.Parent.RemoteEvent:FireServer(pos)
	end
end)
script.Parent.Unequipped:Connect(function()
	off = true
end)
local player = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
local off = false
local tool = script.Parent
local equipped = false
local posx
local posy
local posz

local gridsize = 4

local function snap(clonedbrick)
	posx = math.floor(mouse.Hit.X/gridsize + 0.5) * gridsize
	posy = math.floor(mouse.Hit.Y/gridsize + 0.5) * gridsize
	posz = math.floor(mouse.Hit.Z/gridsize + 0.5) * gridsize
end


tool.Equipped:Connect(function()
	if equipped == false then
		equipped = true
		local clonedbrick = game.ReplicatedStorage.Brick:Clone()
		mouse.TargetFilter = clonedbrick
		mouse.Move:Connect(function()
			snap()
			clonedbrick.Transparency = .5
			clonedbrick.Parent = workspace
			while mouse ~= nil do
				clonedbrick.CFrame = CFrame.new(posx,posy,posz)
				wait()
				if off == true or equipped == false then
					clonedbrick:Destroy()
					off = false
					break
				end
			end
		end)
	end
end)

script.Parent.Activated:Connect(function()
	local BrickToPlace = game.ReplicatedStorage.Brick:Clone()
	local pos = mouse.Hit.p + Vector3.new(posx,posy,posz)
	if(pos - player.Character.HumanoidRootPart.Position).Magnitude <= 12 then
		script.Parent.RemoteEvent:FireServer(pos)
	end
end)

script.Parent.Unequipped:Connect(function()
	if equipped == true then
		equipped = false
		off = true
	end
end)

Maybe cause you are parenting the clonebrick as nil, so it wont change its parent. Try using that

5 Likes

Thank you for the help. I really appreciate it :slight_smile:.

The codes has one problem, It shows an error when I unequip the tool. Screenshot 2020-11-29 131822

If that error comes from my code, can you provide me more information? I would be glad and ready to help!

I clicked on the error from the output. (The highlighted blue part is where the error is coming from)

I also haven’t tackled with the server script yet.

Its cause the parent is being argumented when mouse is moving, so due to we say, when off is true or equipped is false, the parent is nil, cause the brick is destroyed, move out “clonebrick.Parent = workspace” of the mouse.Move function, and place it under the first argument of clonebrick, wich should be local clonebrick…

Thanks, now I don’t have an error.

No problem! I am very glad to help.