The solution is to check if the model:IsA("Model") and if it's the case, add it in workspace.
I went ahead and organized your script. What it does is generate a new model every 3 seconds. It checks if the model exists and if it does parents it in workspace. Then it runs a coroutine to destroy the model after a random amount of time while it can continue generating new models every 3 seconds.
Here's what it looks like:
01 | local InsertService = game:GetService( "InsertService" ) |
03 | local function ModelExists(modelID) |
05 | local success, fail = pcall ( function () |
06 | local model = InsertService:LoadAsset(modelID) |
09 | print ( "Is an existing model" ) |
14 | local function DestroyItem(whichItem) |
15 | wait(math.random( 1 , 100 )) |
19 | local function GenerateModel() |
20 | print ( "Generating new model" ) |
22 | local max = 9999999999 |
23 | local modelID = math.floor(math.random() * (max - min) + min) |
24 | if ModelExists(modelID) then |
25 | local model = InsertService:LoadAsset(modelID) |
27 | if model:IsA( "Model" ) then |
28 | model.Parent = workspace |
29 | coroutine.wrap(DestroyItem)(model) |
Let me know if that answers your question!