Game freezes for a fraction of a second whenever deleting or cloning a model

So I am implementing a mining feature into my game but whenever the player mines an ore, it causes the game to freeze for a few frames.


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

local OreToClone = ReplicatedStorage.Objects.BreakableObjects.Ore.CoalOre


local OreSpawner = {}
OreSpawner.__index = OreSpawner

function OreSpawner.DeleteOre(ore: Instance)
	local OreSpawn = ore.Parent
	OreSpawn.Rotation = Vector3.new(0, math.random(0, 360), 0)
	ore:Destroy()


	local function SpawnOre()
		wait(math.random(20,30))
		local OreClone = OreToClone:Clone()
		OreClone.Parent = OreSpawn
		OreClone:PivotTo(OreSpawn.CFrame)

	end
	SpawnOre()
	return
end

return OreSpawner

On the :Destroy() and Clone() all players have a sudden fps freeze

I have changed to different lighting (I am on voxel right now), made the script client-side, removed the code and just made it transparent which did the same fps drop, removed collisions, removed tags removed the respawning with the long :wait() and nothing seems to work.

I removed the listener also and just made the client destroy it on their side and it would still cause this issue

I just can’t seem to get past this after months of trying. :frowning_face:

making new objects can be expensive, consider using object pooling, i think there’s modules out there that people have already made but idk if they’re good

1 Like

Thanks for this. ChatGPT tried telling me but I dismissed it.

What I have done now in case anybody else experiences this:

Instead of deleting the ore, I teleport it about 1000 studs away which is outside of my map and change the necessary attributes and tags so they it can’t be mined (or do a check on where the player is located in the map to see if they are in the mine etc) and then I just teleport it back.

I noticed that teleporting it super far away around 10000 studs was causing the same problem but I had tested it initially with 100 studs and it worked so I just teleport it outside of the map which for me is a short distance and then bring it back which I can’t notice any performance issues with.

In the example given I found this Part Cache link which was helpful

1 Like

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