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

How do I make this script still loop even after an error?

Asked by 4 years ago

How do I stop it from stopping each time it errors? I'm making a random game advertisement board (for fun) but it keeps on stopping whenever it doesn't find an asset. But I want the script to ignore that and keep going. How do I achieve this?

local marketplace = game:GetService('MarketplaceService')
while wait(3) do
    local min = 1
    local max = 9999999999
    local placeId = math.floor(math.random() * (max - min) + min)
    script.Parent.Texture = "https://www.roblox.com/asset-thumbnail/image?assetId="..placeId.."&width=768&height=432&format=png"
    print(placeId)
    local info = marketplace:GetProductInfo(placeId)
    script.Parent.Parent["Advertise!"].TextLabel.Text = info.Name
end

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

PCall Should Help.

Some Documentation: devforum.roblox.com/t/pcalls-when-and-how-to-use-them/393687 devforum.roblox.com/t/pcall-function/227937 devforum.roblox.com/t/how-to-use-pcall/448345 devforum.roblox.com/t/pcalls-when-and-how-to-use-them/393687

So you could do

local marketplace = game:GetService('MarketplaceService')
while wait(3) do
    local success, response = pcall(function()
        local min = 1
        local max = 9999999999
        local placeId = math.floor(math.random() * (max - min) + min)
        script.Parent.Texture = "https://www.roblox.com/asset-thumbnail/image?assetId="..placeId.."&width=768&height=432&format=png"
        print(placeId)
        local info = marketplace:GetProductInfo(placeId)
        script.Parent.Parent["Advertise!"].TextLabel.Text = info.Name
    end)
end
0
Thanks, I've heard of pcall before but I didn't know it could do that. squidiskool 208 — 4y
0
No Problem! co_existance 141 — 4y
Ad

Answer this question