Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

2 comments

0

Thanks, I've heard of pcall before but I didn't know it could do that.

0

No Problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.