Decal generator help

stops randomly at like 30-90 decals changed

local market = game:GetService("MarketplaceService")

local active = false

local function randomdecal()
		for _, v in pairs (workspace.DecalParts:GetChildren()) do
		local id = math.random(1,10000000)
				
		local decal = v:findFirstChildOfClass("Decal")
		
		local success = false
		pcall(function()
			if market:GetProductInfoAsync(id).AssetTypeId == 1 then
				success = true
								
				if success == true then
					print("decal found :)")
					decal.Texture = "rbxassetid://"..id
				else
					print("fails :p")
					randomdecal()
				end
			end
		end)
	end
end

randomdecal()
2 Likes

What do you mean by stops randomly?

1 Like

it just stops changing the decals? im guessing its because the decals failed but then how would i go about fixing that

Could you provide me with Info about the folder workspace.DecalParts so I can try to recreate it? Like are there just a bunch of random decal objects or smth?


(ignore the click detector and script in this part)

When I do it, it stop around 20. Im guessing there’s some sort of yielding happening indefinitely and some sort of timeout is needed. I’ll try working on it some more.

1 Like

Upon closer inspection there are some issues with your code preventing the fail message from happening–idk if this is intentional or not.

				success = true

				if success == true then
					print("decal found :)")
					decal.Texture = "rbxassetid://"..id
				else
					print("fails :p")
					randomdecal()
				end

since you set success = true right before the if statement it means the if statement will always be true

3 Likes

Alr I figured it out. Whenever the random Id wasn’t valid, it would move on to the next DecalPart without retrying for another random Id. Instead, whenever an Id wasn’t valid it would move on to the next DecalPart and completely skip the previous.

This code works:

local market = game:GetService("MarketplaceService")

local active = false

local function GetAValidRandomDecal(): number
	--Repeats until it finds a valid decal id then returns the id
	while true do
		local id = math.random(1,10000000)
		local success, err = pcall(function() 
			if game.MarketplaceService:GetProductInfoAsync(id).AssetTypeId ~= 1 then
				--if id is valid but isn't assetTypeId 1 then the id isn't valid so it repeats
				error("Not a decal")
			end
		end)

		--if found return
		if success then
			print("decal found :)")
			return id
		end
	end
end

local function randomdecal()
	for _, DecalPart in pairs (workspace.DecalParts:GetChildren()) do
		
		--Creates new thread for each decal part to speed up the script
		--I would highly recomend using Github:Eveara/promise for something like this
		task.spawn(function()
			local id = GetAValidRandomDecal()

			DecalPart:FindFirstChildOfClass("Decal").Texture = "rbxassetid://"..id
		end)
	end
end

randomdecal()
2 Likes

works nicely thank you for helping me

1 Like

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