Breaking block issue in my building game

When i break a block, sometimes when i break a block this error shows up.

Server Code:

-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
	local newBlock = block:Clone()
	newBlock:SetPrimaryPartCFrame(pos)
	newBlock.Name = block.Name
	for i,v in pairs(newBlock:GetChildren()) do
		if newBlock.PrimaryPart ~= v then
			v.CanCollide = true
		else
			v.CanCollide = false
		end
	end
	newBlock.Parent = PlacedBlocks
	local Placer = Instance.new("ObjectValue", newBlock)
	Placer.Name = "Placer"
	Placer.Value = player
end)

RemoveBlock.OnServerEvent:Connect(function(player, block)
	if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then
		if block.Parent ~= PlacedBlocks then
			if block.Parent:FindFirstChild("Placer") then
				if block.Parent.Placer.Value == player then
					block.Parent:Destroy()
				end	
			end
		end
	end
end)

ClearAllBlock.OnServerEvent:Connect(function(player)
	for i,v in pairs(PlacedBlocks:GetChildren()) do
		if v:FindFirstChild("Placer") and v.Placer.Value == player then
			v:Destroy()
		end
	end
end)

Client:

-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local workspace = game.Workspace
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Models = repStorage:WaitForChild("Models")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = workspace.Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = workspace.Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlocks = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
local SelectionBox = repStorage:WaitForChild("RemoveBox")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.TargetFilter = IgnoredBlocks
local target = nil
local pos = nil
local norm = nil
local isEnabled = false
local canPlace = false
local selectedBlock
local modeldata = {}
local selectedpos = 1
local mode = "build"
local MAX_PLACEMENT_DISTANCE = 360
local RemoveBox = RemoveBlock:Clone()
local fakeBlock
-- // MAIN \\ --
function doFakeMouseStuff()
	local ignoreList = {IgnoredBlocks}
	local mouseRay = Mouse.UnitRay
	local newRay = Ray.new(mouseRay.Origin, mouseRay.Direction.unit * MAX_PLACEMENT_DISTANCE)
	target, pos, norm = workspace:FindPartOnRayWithIgnoreList(newRay, ignoreList)
end

game:GetService("RunService").RenderStepped:connect(function()
	doFakeMouseStuff()
end)

function getblockPos(vec, block)
	local posx = math.floor(vec.X / block.PrimaryPart.Size.X + .5) * block.PrimaryPart.Size.X 
	local posy = math.floor(vec.Y / block.PrimaryPart.Size.Y + .5) * block.PrimaryPart.Size.Y
	local posz = math.floor(vec.Z / block.PrimaryPart.Size.Z + .5) * block.PrimaryPart.Size.Z
	return Vector3.new(posx, posy, posz)
end

Mouse.Button1Down:connect(function()
	if isEnabled == true then
		if mode == "build" then -- if the mode is build mode
			PlaceBlock:FireServer(selectedBlock, fakeBlock.PrimaryPart.CFrame) -- fires the event to place a block
		elseif mode == "remove" then -- if the mode is remove mode
			if Mouse.Target ~= nil then -- detects if the Target is nil
				if Mouse.Target.Parent:IsA("Model") then -- detects if the Target instance is a Model
					RemoveBlock:FireServer(Mouse.Target) -- fires the event and remove the block
				end
			end
		end
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.UserInputType ~= Enum.UserInputType.TextInput then
		if input.KeyCode == Enum.KeyCode.X then
			if isEnabled == true then
				if mode == "build" then
					mode = "remove"
				else
					mode = "build"
				end
			end
		end
		if input.KeyCode == Enum.KeyCode.E then
			if isEnabled == true then
				isEnabled = false
			else
				isEnabled = true
			end
		end
		if input.KeyCode == Enum.KeyCode.R then
			local rotation = 0
			if fakeBlock.Parent ~= nil then -- detects if the hoverblock is nil
				fakeBlock:SetPrimaryPartCFrame(fakeBlock.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(rotation + 45), 0)) -- rotates the block
			end
		end
		if input.KeyCode == Enum.KeyCode.RightControl then
			ClearAllBlocks:FireServer() -- deletes the player placed blocks
		end
		if input.KeyCode == Enum.KeyCode.Q then
			if isEnabled == true then
				selectedBlock = Models:WaitForChild(modeldata[selectedpos].Name)
				fakeBlock:Destroy()
				fakeBlock = selectedBlock:Clone()
				if selectedpos == #modeldata then
					selectedpos = 1
				else
					selectedpos = selectedpos + 1
				end
			end
		end
	end
end)

if selectedBlock == nil then
	for i, v in pairs(Models:GetChildren()) do
		table.insert(modeldata, v)
		local number = math.random(1, i)
		selectedBlock = Models:WaitForChild(modeldata[number].Name)
		fakeBlock = selectedBlock:Clone()
	end
end

while wait() do
	if isEnabled == true then
		if fakeBlock.Parent == nil then
			fakeBlock = selectedBlock:Clone()
			fakeBlock.Parent = IgnoredBlocks
		end
		if mode == "build" then
			RemoveBox:Destroy()
			if fakeBlock.Parent == nil then
				fakeBlock = selectedBlock:Clone()
				fakeBlock.Parent = IgnoredBlocks
			end
			if fakeBlock and pos and norm then
				for i,v in pairs(fakeBlock:GetChildren()) do
					v.CanCollide = false
					v.Transparency = .6
					if i > 1 then
						fakeBlock.PrimaryPart.Transparency = 1
					end
					if Mouse.Target ~= nil then
						v.Color = Color3.fromRGB(9, 137, 207)
						canPlace = true
					else
						v.Color = Color3.fromRGB(170, 0, 0)
						canPlace = false
					end
				end
				doFakeMouseStuff()
				local endPos = getblockPos(pos + (norm * (fakeBlock.PrimaryPart.Size * .5)), fakeBlock)
				fakeBlock:SetPrimaryPartCFrame(CFrame.new(endPos) * CFrame.Angles(0, math.rad(fakeBlock.PrimaryPart.Orientation.Y), 0))
			end
		elseif mode == "remove" then
			fakeBlock:Destroy()
			if RemoveBox.Parent == nil then
				RemoveBox = SelectionBox:Clone()
				RemoveBox.Parent = IgnoredBlocks
			end
			if Mouse.Target ~= game.Workspace.Base and Mouse.Target ~= nil then
				if Mouse.Target.Parent:IsA("Model") and Mouse.Target.Parent.PrimaryPart ~= nil and Mouse.Target.Parent:FindFirstChild("Placer") then
					RemoveBox:SetPrimaryPartCFrame(Mouse.Target.Parent.PrimaryPart.CFrame)
				else
					RemoveBox:Destroy()
				end
			else
				RemoveBox:Destroy()
			end
		end
	else
		if selectedBlock ~= nil then
			fakeBlock:Destroy()
			RemoveBox:Destroy()
		end
	end
	if fakeBlock.Parent ~= nil or RemoveBox.Parent ~= nil then
		if Player.Character.Humanoid.Health < 1 then
			game:GetService("Debris"):AddItem(fakeBlock and RemoveBox, 0)
			isEnabled = false
		end
	end
end

Is placeServer the server code you showed me? I’m not seeing anything but an ‘end)’ on line 38 so perhaps ClearAllBlock is nil?

That error means you are trying to use WaitForChild like Instance:WaitForChild(nil) or nil:WaitForChild(...) which is improper usage.

oh sorry the screenshot is wrong. please review again.

Check for the path of the block value that you are passing over to the server.

This line could perhaps be the culprit since it could be letting in a part that doesn’t exist or was created on the client.

if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then

A possible fix:

if block.Parent == workspace and block ~= "Base" then

uhh… it didn’t work well because when i break a block it doesn’t break the block

change your code to this

-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
	local newBlock = block:Clone()
	newBlock:SetPrimaryPartCFrame(pos)
	newBlock.Name = block.Name
	for i,v in pairs(newBlock:GetChildren()) do
		if newBlock.PrimaryPart ~= v then
			v.CanCollide = true
		else
			v.CanCollide = false
		end
	end
	newBlock.Parent = PlacedBlocks
	local Placer = Instance.new("ObjectValue")
	Placer.Parent = newBlock
	Placer.Name = "Placer"
	Placer.Value = player
end)

RemoveBlock.OnServerEvent:Connect(function(player, block)
	if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then
		if block.Parent ~= PlacedBlocks then
			if block.Parent:WaitForChild("Placer") then
				if block.Parent.Placer.Value == player then
					block.Parent:Destroy()
				end	
			end
		end
	end
end)

ClearAllBlock.OnServerEvent:Connect(function(player)
	for i,v in pairs(PlacedBlocks:GetChildren()) do
		if v:FindFirstChild("Placer") and v.Placer.Value == player then
			v:Destroy()
		end
	end
end)

still not working
(30 characters)

-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
	local newBlock = block:Clone()
	newBlock:SetPrimaryPartCFrame(pos)
	newBlock.Name = block.Name
	for i,v in pairs(newBlock:GetChildren()) do
		if newBlock.PrimaryPart ~= v then
			v.CanCollide = true
		else
			v.CanCollide = false
		end
	end
	newBlock.Parent = PlacedBlocks
	local Placer = Instance.new("ObjectValue")
	Placer.Parent = newBlock
	Placer.Name = "Placer"
	Placer.Value = player.Name
end)

RemoveBlock.OnServerEvent:Connect(function(player, block)
	if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then
		if block.Parent ~= PlacedBlocks then
			if block.Parent:WaitForChild("Placer") then
				if block.Parent.Placer.Value == player then
					block.Parent:Destroy()
				end	
			end
		end
	end
end)

ClearAllBlock.OnServerEvent:Connect(function(player)
	for i,v in pairs(PlacedBlocks:GetChildren()) do
		if v:FindFirstChild("Placer") and v.Placer.Value == player then
			v:Destroy()
		end
	end
end)

You can’t set a value to a name otherwise it will be nil

Still not working?

1 Like

still had the error
(30 chars)

Where did you referenced the block

and i changed the code to this:

-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
	local newBlock = block:Clone()
	newBlock:SetPrimaryPartCFrame(pos)
	newBlock.Name = block.Name
	for i,v in pairs(newBlock:GetChildren()) do
		if newBlock.PrimaryPart ~= v then
			v.CanCollide = true
		else
			v.CanCollide = false
		end
	end
	newBlock.Parent = PlacedBlocks
	local Placer = Instance.new("ObjectValue", newBlock)
	Placer.Name = "Placer"
	Placer.Value = player
end)

RemoveBlock.OnServerEvent:Connect(function(player, block)
	if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then
		if block.Parent ~= PlacedBlocks then
			if block.Parent:FindFirstChild("Placer") then
				if block.Parent.Placer.Value == player then
					block.Parent:Destroy()
				end	
			end
		end
	end
end)

ClearAllBlock.OnServerEvent:Connect(function(player)
	for i,v in pairs(PlacedBlocks:GetChildren()) do
		if v:FindFirstChild("Placer") and v.Placer.Value == player then
			v:Destroy()
		end
	end
end)

into this:

-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
	local newBlock = block:Clone()
	newBlock:SetPrimaryPartCFrame(pos)
	newBlock.Name = block.Name
	for i,v in pairs(newBlock:GetChildren()) do
		if newBlock.PrimaryPart ~= v then
			v.CanCollide = true
		else
			v.CanCollide = false
		end
	end
	newBlock.Parent = PlacedBlocks
	local Placer = Instance.new("StringValue", newBlock)
	Placer.Name = "Placer"
	Placer.Value = player.Name
end)

RemoveBlock.OnServerEvent:Connect(function(player, block)
	if block ~= nil and block ~= "Workspace" and block ~= "Base" then
		if block.Parent ~= PlacedBlocks then
			if block.Parent:FindFirstChild("Placer") or block:FindFirstChild("Placer") then
				if block.Parent.Placer.Value == player.Name then
					block.Parent:Destroy()
				end	
			end
		end
	end
end)

ClearAllBlock.OnServerEvent:Connect(function(player)
	for i,v in pairs(PlacedBlocks:GetChildren()) do
		if v:FindFirstChild("Placer") and v.Placer.Value == player.Name then
			v:Destroy()
		end
	end
end)

does it work or not if not then your client must be having some issues

i was thinking about the Mouse.Target

change it to Mouse.Hit.p

Also why is the ignored block the mouse`s target filter

because the ignoredblock children are RemoveBox and blockHighlight(idk how to call that)

pretty sure that the argument block does not exist probably

Do a check if the mouse target is targeting the actual block then fire it to the server or else you are gonna be stuck with this error

1 Like

im just doing that right now
(30 chars)

You cannot set a ValueBase object’s value to a player object, if that’s what you’re doing.

@J0hnLennonPlayz try using mouse.Target if you want to get the BasePart the mouse is pointing at.

If you want to get the CFrame of the mouse’s position, use mouse.Hit.p

1 Like

I already told him to do that but what if their hit is nil then it will give an error still