My game's badge awarder's start lagging after repetitively warning me that people already have the badge. How do I fix that?

Hello! I’ve been working on a game that’s like a badge hunt, but it’s randomized and every badge falls from the sky. [It’s kinda like a lag test game howerver.] Lets say a player touches it, a player should first, get the badge, then the block self-destructs. While it does work, it really gets laggy, and it stops registering people touching the brick, and it takes many minutes for it to register, and I feel like it might be because people already touching it are clogging up things.

How would I make a system where players who already have the badge can’t activate the script, while players who don’t have it can activate the script? [Script Below]

function OnTouch(part)
	if (part.Parent:FindFirstChild("Humanoid") ~= nil) then
		local p = game.Players:GetPlayerFromCharacter(part.Parent)
		local h = part.Parent:FindFirstChild("Humanoid")
		if (p ~= nil) and h.Health > 0 then 
			local b = game:GetService("BadgeService")
			b:AwardBadge(p.userId, ExampleBadgeID)
			script.Parent:Destroy()
		end
	end
end

script.Parent.Touched:connect(OnTouch)

You need to check if the player already owns the badge before giving it to them, im sure theres a function called UserOwnBadgeAsync or just wrap that inside a pcall

pcall(function()
 b:AwardBadge(p.UserId,ExampleBadgeID)
script.Parent:Destroy()
end)

You can use https://developer.roblox.com/en-us/api-reference/function/BadgeService/UserHasBadgeAsync , something like:

 if not BadgeService:UserHasBadgeAsync(player.UserId, badgeid) then
 --award badge
 print("awarded badge!")
else
print("didn't award badge! plr already has the badge")
end

1 Like

Please don’t abuse pcalls like this. Not only is this a warning, not an error, even if it did error you should use the actual function that lets you check instead of a hacky pcall method.

1 Like