Issue with my block breaking script

local Blocks = {
	["GrassBlock"] = { Sound = 6496157434, SideTexture = 9267155972, Texture = 9267183930, Size = Vector3.new(4, 4, 4), IntValue = "GrassBlockAmount", BreakPoints = 10, CanBeBroken = true, BlockID = "grass"},
	["StoneBlock"] = { Sound = 6496157434, SideTexture = 8676746437, Texture = 8676746437, Size = Vector3.new(4, 4, 4), IntValue = "AmountOfStone", BreakPoints = 20, CanBeBroken = true, BlockID = "stone"},
	["DirtBlock"] = { Sound = 6496157434, SideTexture = 7766004119, Texture = 7766004119, Size = Vector3.new(4, 4, 4), IntValue = "AmountOfDirt", BreakPoints = 10, CanBeBroken = true, BlockID = "dirt"},
	["CoalBlock"] = { Sound = 6496157434, SideTexture = 11425628837, Texture = 11425628837, Size = Vector3.new(4, 4, 4), IntValue = "CoalAmount", BreakPoints = 25, CanBeBroken = true, BlockID = "coal_ore_block"},
	["IronBlock"] = { Sound = 6496157434, SideTexture = 11425626868, Texture = 11425626868, Size = Vector3.new(4, 4, 4), IntValue = "RawIronAmount", BreakPoints = 30, CanBeBroken = true, BlockID = "iron_ore_block" },
	["Bedrock"] = { Sound = 6496157434, SideTexture = 12252439624, Texture = 12252439624, Size = Vector3.new(4, 4, 4), IntValue = "AmountOfBedrock", BreakPoints = 1000, CanBeBroken = false, BlockID = "bedrock" }
}
local TS = game:GetService("TweenService")

local function CreateBlock(name, x, y, z)
	local blockData = Blocks[name]
	if blockData then
		local block = Instance.new("Part")
		block.Parent = game.Workspace.Assets.Land
		block.Name = name .. "_" .. x .. "_" .. y .. "_" .. z
		print(name .. "_" .. x .. "_" .. y .. "_" .. z .. " created")
		block.Size = blockData.Size
		block.Position = Vector3.new(x, y + block.Size.Y / 2, z)
		block.Anchored = true
		block.CanCollide = true
		for e = 1, 2 do
			local texture = Instance.new("Decal")
			texture.Parent = block
			texture.Texture = "rbxassetid://" .. blockData.Texture
			if e == 1 then
				texture.Face = Enum.NormalId.Top
			else
				texture.Face = Enum.NormalId.Bottom
			end
		end

		for p = 1, 4 do
			local texture = Instance.new("Decal")
			texture.Parent = block
			texture.Texture = "rbxassetid://" .. blockData.SideTexture
			if p == 1 then
				texture.Face = Enum.NormalId.Right
			elseif p == 2 then
				texture.Face = Enum.NormalId.Left
			elseif p == 3 then
				texture.Face = Enum.NormalId.Front
			elseif p == 4 then
				texture.Face = Enum.NormalId.Back
			end
		end
		
		local CurrentBreakPoints = Instance.new("IntValue")
		CurrentBreakPoints.Parent = block
		CurrentBreakPoints.Name = "CurrentBreakPoints"
		CurrentBreakPoints.Value = 0
		if name == "GrassBlock" then
			local isSnowy = Instance.new("BoolValue")
			isSnowy.Parent = block
			isSnowy.Name = "IsSnowy"
			isSnowy.Value = false
		end
		
		local click = Instance.new("ClickDetector")
		click.Parent = block

		click.MouseClick:Connect(function(plr)
			local sound = Instance.new("Sound", block)
			sound.SoundId = "rbxassetid://" .. blockData.Sound
			sound:Play()
			task.wait(1)
			sound:Destroy()
			CurrentBreakPoints.Value = CurrentBreakPoints.Value + 2
			if blockData.CanBeBroken and CurrentBreakPoints.Value >= blockData.BreakPoints then
				local clone = block:Clone()
				block:Destroy()
				clone.Size = Vector3.new(1, 1, 1)
				clone.Position = Vector3.new(block.Position.X, block.Position.Y - 0.54664, block.Position.Z)
				clone.Parent = game.Workspace.Assets.DroppedItems
				clone.CanCollide = false
				click:Destroy()
				local Info = TweenInfo.new(
					1,
					Enum.EasingStyle.Linear,
					Enum.EasingDirection.Out,
					math.huge,
					false,
					0
				)
				local Goal = {
					CFrame = clone.CFrame * CFrame.Angles(0, math.rad(90), 0)
				}
				local Tween = TS:Create(clone, Info, Goal)
				Tween:Play()
				clone.Touched:Connect(function(touch)
					clone:Destroy()
					local character = touch.Parent
					local plr = game.Players:GetPlayerFromCharacter(character)
					if plr then
						local stringInt = plr:FindFirstChild(blockData.IntValue)
						if stringInt then
							local value = stringInt.Value
							stringInt.Value = value + 1
						end
					end
				end)
			end
		end)
	end
end
local centerX, centerY, centerZ = 0, 2, 0
local chunkSize = 7
local halfSize = math.floor(chunkSize / 2)
local function CreateChunk(chunkX, chunkZ)
	for x = centerX - (halfSize * 4) + (chunkX * chunkSize * 4), centerX + (halfSize * 4) + (chunkX * chunkSize * 4), 4 do
		for y = centerY - (halfSize * 4), centerY + (halfSize * 4), 4 do
			for z = centerZ - (halfSize * 4) + (chunkZ * chunkSize * 4), centerZ + (halfSize * 4) + (chunkZ * chunkSize * 4), 4 do
				if y == centerY then
					CreateBlock("Bedrock", x, y, z)
				elseif y == 6 then
					CreateBlock("StoneBlock", x, y, z)
				elseif y == 10 then
					CreateBlock("DirtBlock", x, y, z)
				elseif y == 14 then
					CreateBlock("GrassBlock", x, y, z)
				end
			end
		end
	end
end
for chunkX = -1, 1 do
	for chunkZ = -1, 1 do
		CreateChunk(chunkX, chunkZ)
	end
end

Issue(s):
1.
when mining 1 block, it creates 2 dropped items (instead of 1) and 1 of them has all the textures and stuff, one doesn’t
image
image
2. it randomly drops multiple amounts of these random gray blocks (1 block made 5 of them)
image
and when any of them are touched it gives me all the values of them…when it’s not supposed to-
image

Any idea how to fix?
THANKS TO THE PEOPLE HELPING, YOU ARE AMAZING

1 Like
  1. Try introducing a print statement and make sure your click event isn’t firing twice. If it is, try implementing a debounce or check so that you don’t break the same block twice.

For number three, It seems the clone.Touched event is connected so that one of them will trigger and give you all of them, I could probably help more with that later, get back to me once you try it our

I found out the issue, since I put the task.wait(1) was causing the block to be made mutiple times since I spammed it

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