Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make this RANDOM model inserter only find models from the Library?

Asked by 3 years ago
Edited 3 years ago

I'm having a problem trying to make this chaotic game where it inserts a random model from the Library every 3 seconds. Then, at a random interval, it removes that random model. But never wants to find model assets. All it finds are other assets. How do I make it so it only finds models?

local InsertService = game:GetService("InsertService")
while wait(3) do
    local success, response = pcall(function()
        local min = 1
        local max = 9999999999
        local modelID = math.floor(math.random() * (max - min) + min)
        local model = InsertService:LoadAsset(modelID)
        model.Parent = workspace
        wait(math.random(1,100))
        model:Destroy()
    end)
end

1 answer

Log in to vote
1
Answered by 3 years ago

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:

local InsertService = game:GetService("InsertService")

local function ModelExists(modelID)
    local model
    local success, fail = pcall(function()
        local model = InsertService:LoadAsset(modelID)
    end)
    if success then
        print("Is an existing model")
        return true
    end
end

local function DestroyItem(whichItem)
    wait(math.random(1,100))
    whichItem:Destroy()
end

local function GenerateModel()
    print("Generating new model")
    local min = 1
    local max = 9999999999
    local modelID = math.floor(math.random() * (max - min) + min)
    if ModelExists(modelID) then
        local model = InsertService:LoadAsset(modelID)
        -- If you want other things as well then simply add 'or model:IsA("Part")' 'or model:IsA("UnionOperation")'
        if model:IsA("Model") then
            model.Parent = workspace
            coroutine.wrap(DestroyItem)(model)
        else
            GenerateModel()
        end
    end
end

while wait(3) do
    GenerateModel()
end

Let me know if that answers your question!

0
I sorta abandoned this quickly because I realesed InsertService has this really bad limitation where it can only insert models made by Roblox and the creator. So yeah. squidiskool 208 — 3y
Ad

Answer this question