Parts glitching through even though I'm checking for the position beforehand?

Hello! I’ve used S_maritan’s tutorial to make a simple ore generation system, it works fine when I use click detectors, but when I use my main script to fire a remote event (partdelete) parts start spawning into one another and glitching. Any thoughts on what could cause the problem?

Here’s the code

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);
}

function Generate(pos)
	if pos then
		for _,pos2 in pairs(positions) do
			local newPos = pos+pos2
			local previouslyGenerated
			for _,generated in pairs(workspace.Cubes.Generated:GetChildren()) do
				if generated.Value == newPos then
					previouslyGenerated = true
				end
			end
			
			if newPos.Y > 0 then
				previouslyGenerated = true
			end
			
			if not previouslyGenerated then
				local ore = game.ServerStorage.Ores.Stone:Clone()
				ore.Position = newPos
				ore.Parent = workspace.Cubes
				local val = Instance.new("Vector3Value")
				val.Value = newPos
				val.Parent = workspace.Cubes.Generated
			end
		end
	end
end

for i,cube in pairs(workspace.Cubes:GetChildren()) do
	if cube:IsA("Part") then
		local val = Instance.new("Vector3Value")
		val.Value = cube.Position
		val.Parent = workspace.Cubes.Generated
	end
end

game.ReplicatedStorage.partdelete.OnServerEvent:Connect(function(plr, ore)
	
	local val = Instance.new("Vector3Value")
	val.Value = ore.Position
	val.Parent = workspace.Cubes.Generated
	
	Generate(ore.Position)
	ore:Destroy()
end)

Thanks!

I really don’t see any problem. Could you record it happening?

You mention the issue is with a RemoteEvent instance but this script has no FireServer() or FireClient() function call within it.

the code loops through every face of the generated ore, and cloning a new part each time, so that is how it was meant to be…

i have just realized you are checking it each time, try printing (newPos) and (generated.Value) and see what you get

this is what’s causing the problem