Block Generation system placing blocks where they shouldn't be

I was scripting some extra stuff into a mining system I made for my game, but all the sudden it broke and it starts putting blocks in places where they shouldn’t be, and I don’t know what’s going wrong because I changed nothing for this to happen as far as I’m aware.

Here’s the code if anyone could help I’d appreciate it.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local POSITIONS = {
	Vector3.new(6, 0, 0),
	Vector3.new(-6, 0, 0),
	Vector3.new(0, 6, 0),
	Vector3.new(0, -6, 0),
	Vector3.new(0, 0, 6),
	Vector3.new(0, 0, -6),
}

local Layers = {
	ServerStorage.LayerBlocks.Stone,
	ServerStorage.LayerBlocks.Limestone,
	ServerStorage.LayerBlocks.Granite,
	ServerStorage.LayerBlocks.Diorite,
	ServerStorage.LayerBlocks.Sandstone,
	ServerStorage.LayerBlocks.Obsidian,
	ServerStorage.LayerBlocks["Cracked Lava"],
	ServerStorage.LayerBlocks.Magma,
	ServerStorage.LayerBlocks.Bedrock,
	ServerStorage.LayerBlocks["Compressed Lava"],
	ServerStorage.LayerBlocks["Unbreakable Steel"]
}

local OreTable = {}

for Index, Inst in ipairs(ServerStorage.Ores:GetChildren()) do
	if Inst:IsA("Part") then
		table.insert(OreTable, {oBlock = Inst, chance = Inst.Chance.Value})
	end
end

local function generateOres(position, depth)
	if not position then
		return
	end

	local generated = workspace.Generated
	for _, offset in ipairs(POSITIONS) do
		local newPosition = position + offset
		if newPosition.Y > 0 then
			continue
		end
		if generated:FindFirstChild(tostring(newPosition)) then
			continue
		end
		local random = math.random(1, #OreTable)
		local willSpawn = math.random(1, OreTable[random].chance)
		if willSpawn == 1 then
			ore = OreTable[random].oBlock:Clone()
			if OreTable[random].chance >= 1000000000 then
				ReplicatedStorage.EventText:FireAllClients("Mythical")
				ReplicatedStorage.Sounds.Unim:Play()
			end
		else
			if depth == 0 then
				ore = Layers[1]:Clone()
			else
				ore = Layers[math.ceil(depth / 1000)]:Clone()
			end
		end
		ore.Position = newPosition
		ore.Parent = workspace.Cubes
		local value = Instance.new("Vector3Value")
		value.Value = newPosition
		value.Name = tostring(newPosition)
		value.Parent = generated
	end
end

local generated = workspace.Generated
for _, cube in ipairs(workspace.Cubes:GetChildren()) do
	if cube:IsA("Part") then
		local value = Instance.new("Vector3Value")
		value.Value = cube.Position
		value.Name = tostring(cube.Position)
		value.Parent = generated
	end
end

ReplicatedStorage.BlockGen.OnServerEvent:Connect(function(_, rPart)
	generateOres(rPart.Position, math.abs(rPart.CFrame.Y / 6))
	rPart:Destroy()
end)

Show us a picture or something. So we can see what the issue looks like.

image
It just ignores the values in the generated folder which holds cframevalues to not spawn blocks there

bump :3
(i still dont know what is goiung wrong at all)

another bump I still haven’t solved it because I was busy doing other stuff

I fixed it by making a new function that determines what block to spawn.

local function determineBlock(depth)
	local random = math.random(1, #OreTable)
	local willSpawn = math.random(1, OreTable[random].chance)
	
	if willSpawn == 1 then
		local ore = OreTable[random].oBlock:Clone()
		if OreTable[random].chance >= 1000000000 then
			ReplicatedStorage.EventText:FireAllClients("Mythical")
		end
		return ore
	else
		if depth == 0 then
			local ore = Layers[1]:Clone()
			return ore
		else
			local ore = Layers[math.ceil(depth / 1000)]:Clone()
			return ore
		end
	end
end

and made the ore variable in the function that actually spawns the block the first function that determines it.

local function generateOres(position, depth)
	if not position then
		return
	end

	local generated = workspace.Generated
	for _, offset in ipairs(POSITIONS) do
		local newPosition = position + offset
		if newPosition.Y > 0 then
			continue
		end
		if generated:FindFirstChild(tostring(newPosition)) then
			continue
		end
		local ore = determineBlock(depth)
		ore.Position = newPosition
		ore.Parent = workspace.Cubes
		local value = Instance.new("Vector3Value")
		value.Value = newPosition
		value.Name = tostring(newPosition)
		value.Parent = generated
	end
end

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