Cloning Random instances or any instance from a folder with :Clone() clears them from the folder, how can i make them infinite?

Hello! This is a part from one of my scripts that spawns a random NPC every 60 seconds from a folder that is located in the ServerStorage, but whenever it clones the chosen NPC it takes the instance out from the folder and places it into the workspace, so when it loops around it keeps taking every NPC from the folder until there are none left, which it then stops working.

	local NPCPool = game:GetService("ServerStorage"):WaitForChild("NPCS"):GetChildren()
	local Randomness = math.random(1,#NPCPool)
	local ChosenNPC = NPCPool[Randomness]
	ChosenNPC:Clone().Parent = workspace
	task.wait(60)	
	ChosenGuest:Destroy()
	game.Workspace.Poof:Play()

Is there a method to make the instances not get taken directly from the folder (making them infinite) so it can sucessfully loop forever?

1 Like

Could be because you’re destroying the original model in ServerStorage, instead of the cloned model. I edited the code below, lmk if it works

local NPCPool = game:GetService("ServerStorage"):WaitForChild("NPCS"):GetChildren()
local Randomness = math.random(1,#NPCPool)
local ChosenNPC = NPCPool[Randomness]
local cloneNpc = ChosenNPC:Clone()
cloneNpc.Parent = workspace
task.wait(60)	
cloneNpc:Destroy()
game.Workspace.Poof:Play()

That does it now, thanks a lot!

1 Like

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