Mining blocks spawns new blocks on top of not mined blocks

I’m trying to make a minesweeper but it’s a mining game but somewhy blocks just keep on spawning on top of each other. I tried to make minimum distance of 0.01 so it would actually detect other blocks but it doesnt work.

Link to game: Miningsweeper - Roblox

This is Server side code:

for z=-100,100,4 do
	for x=-100,100,4 do
		local tile = game.ServerStorage.Tile:Clone()
		tile.CFrame = CFrame.new(x,10000,z)
		tile.Bomb.Value = math.random() < 0.1
		tile.Parent = workspace.Tiles
	end
end
local colors = {
	[0] = Color3.new(0.75,0.75,0.75),
	Color3.new(0,0,1),
	Color3.new(0,0.75,0),
	Color3.new(1,0,0),
	Color3.new(0,0,0.5),
	Color3.new(0.5,0,0),
	Color3.new(0,0.5,0.5),
	Color3.new(0.5,0,0.5),
	Color3.new(0.5,0.5,0.5),
	Color3.new(0.75,0.75,0),
	Color3.new(0.5,0,1),
	Color3.new(1,0.5,0),
}
game.ReplicatedStorage.MineBlock.OnServerEvent:Connect(function(plr,block)
	if not block or not block.Parent or not block:IsA("BasePart") or block.Name ~= "Tile" then
		return
	end
	if not plr:FindFirstChild("Debounce") then
		local debounce = Instance.new("BoolValue",plr)
		debounce.Name = "Debounce"
		game.Debris:AddItem(debounce,0.9)
		local blocks = workspace.Tiles:GetChildren()
		local mines = 0
		for z=-4,4,4 do
			for y=-4,4,4 do
				for x=-4,4,4 do
					if Vector3.new(x,y,z) ~= Vector3.zero and block.Position.Y+y < 10002 then
						local block_placed = nil
						for _,part in pairs(blocks) do
							if (part.Position-block.Position+Vector3.new(x,y,z)).Magnitude < 0.01 then
								block_placed = part
								break
							end
						end
						if block_placed and block_placed.Bomb.Value then
							mines += 1
						else
							local tile = game.ServerStorage.Tile:Clone()
							tile.CFrame = CFrame.new(x,y,z)+block.Position
							tile.Bomb.Value = math.random() < 0.1
							tile.Parent = workspace.Tiles
							if tile.Bomb.Value then
								mines += 1
							end
						end
					end
				end
			end
		end
		for _,decal in pairs(block:GetChildren()) do
			if decal:IsA("Decal") then
				decal:Destroy()
			end
		end
		block.Transparency = 1
		block.CanCollide = false
		block.CanQuery = false
		if block.Bomb.Value then
			block.Mine.Enabled = true
			task.delay(1,function()
				block.Mine.ImageLabel.ImageTransparency = 0.75
				local boom = Instance.new("Explosion",workspace)
				boom.Position = block.Position
				boom.BlastRadius = 50
			end)
		else
			block.Number.Enabled = true
			block.Number.TextLabel.Text = mines
			block.Number.TextLabel.TextColor3 = colors[mines%12]
		end
		for _=1,16 do
			local particle = Instance.new("Part",workspace)
			particle.CFrame = block.CFrame*CFrame.new(Vector3.new(math.random()-0.5,math.random()-0.5,math.random()-0.5)*4)*CFrame.Angles(math.random()*math.pi*2,math.random()*math.pi*2,math.random()*math.pi*2)
			particle.Size = Vector3.new(math.random(),math.random(),math.random())
			particle.AssemblyLinearVelocity = CFrame.Angles(math.random()*math.pi*2,math.random()*math.pi*2,0).LookVector*math.random()*32
			particle.AssemblyAngularVelocity = CFrame.Angles(math.random()*math.pi*2,math.random()*math.pi*2,0).LookVector*math.random()*10
			game.Debris:AddItem(particle,1)
		end
	end
end)
1 Like

Nevermind i solved it by separating “block_placed and block_placed.Bomb.Value” into 2 if statements and it works now

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