Is my pcall loop terrible?

Greetings!

I made a pcall checking for a Badge UserHasBadgeAsync. According to the official documentation, it returns it and just doesn’t do anything. What I want to try doing is looping until it successfully does it, with a 2 second wait. Is this bad? If so, please explain why.

Thanks in advance! I will attach my code below.
Sincerely, Willi


task.spawn(function() -- Doing this so it can do other code.
	while true do
		local Success, HasBadge = pcall(function()
			return BadgeService:UserHasBadgeAsync(Player.UserId, 2149014904)
		end)
		if Success then
			if not HasBadge then
				BadgeService:AwardBadge(Player.UserId, 2149014904)
			end
			break
		else
			task.wait(2)
		end
	end
end)
2 Likes

I wouldn’t say it is bad, but I will advise that you implement some kinda system such that it will only do it for X tries and then error. Also what’s the use case?

1 Like

Just curious, what is wrong with doing it an infinite number of times. I want to guarantee that the player gets the badge since they rightfully earned it.

Ye I forgot to ask for the use case, in that case, ye I don’t see any issues, however do check if the player left the game and break the loop accordingly.

1 Like

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