HTTP service overloaded even though it shouldn't?

I have this badge script that is supposed to check for when a player’s stats have changed, and, if the player’s stats reached a certain point, give them a badge if they don’t already have it.

However, even with these checks, it apparently doesn’t work and overflows the HTTP service system, which affects other things in the game. No one in the servers of my game I’ve entered has reached the necessary requirements to get the badge, so it shouldn’t be checking for it.

Here’s the code:

local bdg = game:GetService("BadgeService")
local mkt = game:GetService("MarketplaceService")

game.Players.PlayerAdded:connect(function(p) 
	local temp = p:WaitForChild("temp", 2)
	local stats = p:WaitForChild("leaderstats", 2)
	stats.Money.Changed:connect(function() -- money rewards
		if stats.Money.Value >= 250000 then
			if not mkt:PlayerOwnsAsset(p, 2124443166) then
				bdg:AwardBadge(p.userId, 2124443166)  --- big bucks
			end	
		elseif stats.Money.Value >= 7500000 and temp.Kills.Value >= 500 then
			if not mkt:PlayerOwnsAsset(p.userId, 2124443165) then --nuclear player
				bdg:AwardBadge(p.userId, 2124443165)
			end	
		end	
	end)
end)

Here’s a screenshot of the in-game dev console output:

1 Like

Try replacing MarketplaceService:PlayerOwnsAsset with BadgeService:UserHasBadgeAsync.

Also, if the money value is updating constantly, you should look into caching the results of UserHasBadgeAsync to decrease unnecessary API requests.

1 Like

How would/should I cache the results of it? With the way the code is written, it shouldn’t be constantly being checked.

Since your changed event is inside the PlayerAdded event, this should be relatively easy.

When a player first joins, check if they have the badge and create the cache:

local cache = {
	big_bucks = bdg:UserHasBadgeAsync(p.UserId, 2124443166),
	nuclear_player = bdg:UserHasBadgeAsync(p.UserId, 2124443165)
}

In the changed event, replace the line with the API request to if not cache.BADGE_NAME then (Replace BADGE_NAME with either big_bucks or nuclear_player).

When the player earns a badge, set cache.BADGE_NAME to true (again replacing BADGE_NAME).

Full Script
local bdg = game:GetService("BadgeService")

game.Players.PlayerAdded:connect(function(p)
	local cache = {
		big_bucks = bdg:UserHasBadgeAsync(p.UserId, 2124443166),
		nuclear_player = bdg:UserHasBadgeAsync(p.UserId, 2124443165)
	}
	local temp = p:WaitForChild("temp") -- Remove the timeout, as it will only cause an error if the "temp" value fails to exist in 2 seconds.
	local stats = p:WaitForChild("leaderstats")
	stats.Money.Changed:connect(function() -- money rewards
		if stats.Money.Value >= 250000 then
			if not cache.big_bucks then
				bdg:AwardBadge(p.userId, 2124443166)  --- big bucks
				cache.big_bucks = true
			end	
		elseif stats.Money.Value >= 7500000 and temp.Kills.Value >= 500 then
			if not cache.nuclear_player then --nuclear player
				bdg:AwardBadge(p.userId, 2124443165)
				cache.nuclear_player = true
			end	
		end	
	end)
end)

So would it look like this?

local bdg = game:GetService("BadgeService")

game.Players.PlayerAdded:connect(function(p) 
	local temp = p:WaitForChild("temp", 2)
	local stats = p:WaitForChild("leaderstats", 2)
	local cache = {
		big_bucks = bdg:UserHasBadgeAsync(p.UserId, 2124443166),
		nuclear_player = bdg:UserHasBadgeAsync(p.UserId, 2124443165)
	}
	stats.Money.Changed:connect(function() -- money rewards
		if stats.Money.Value >= 250000 then
			if not cache.big_bucks then
				bdg:AwardBadge(p.userId, 2124443166)  --- big bucks
				cache.big_bucks = true
			end	
		elseif stats.Money.Value >= 7500000 and temp.Kills.Value >= 500 then
			if not cache.nuclear_player then --nuclear player
				bdg:AwardBadge(p.userId, 2124443165)
				cache.nuclear_player = true
			end	
		end	
	end)
end)

Eh, it was to make sure it didn’t lag the game. I think I’ll just remove it, since as you said, it’ll probably cause more problems than fix.

Once again thank you!