Is there a way to fix a mass Internal error wave from :UserHasBadgeAsync when joigning?

As a developer of Vehicle tyccon, we currently handle issues with datastore etc. But those warning come retitively. Is there a way to fix thoses?

Module Used:

local module = {}

local BadgeService = game:GetService("BadgeService")

function module:UserHasBadgeAsync(userId, badgeId)
	local success,HasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(userId, badgeId)
	end)

	if not success then
		warn("Error while checking if user has badge:", HasBadge)
		return
	else
		return HasBadge
	end
end

function module:AwardBadge(userId, badgeId)
	-- Check if already awarded
	task.wait(math.random(0.1,0.3))
	
	if self:UserHasBadgeAsync(userId,badgeId) then
		return
	end

    -- Award the badge
	local success,awarded = pcall(function()
		return BadgeService:AwardBadge(userId, badgeId)
	end) 

	if not success then
		warn("Error while awarding badge:", awarded)
		return
	end
end


return module

Server calling script:

-- Updates Client Car Inventory, amount of cars owned
function UpdateCars(Player)
	if PlayerStats:FindFirstChild(Player.Name) ~= nil then
		local Folder = PlayerStats[Player.Name]
		for i, getCars in pairs(Folder.CarInventory:GetChildren()) do
			-- Update Car Inventories
			getCars:GetPropertyChangedSignal("Value"):Connect(function()
				local Cars = Player.leaderstats[tostring("🚗 Cars")]
				Cars.Value = Cars.Value + 1
				UpdateCarEvent:FireClient(Player, getCars.Name, Cars.Value)
				UpdateCarLevelEvent:FireClient(Player, getCars.Name, getCars.Level.Value)
				--[[
				if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, BadgeIDs[getCars.Name]) then
					-- Award Car Badge
					BadgeServiceModule:AwardBadge(Player.UserId, BadgeIDs[getCars.Name])
				end--]]
				BadgeServiceModule:AwardBadge(Player.UserId, BadgeIDs[getCars.Name])

				if Cars.Value >= 10 then -- Own 10 cars
					--[[
					if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124451054) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124451054)
					end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124451054)
				end
				if Cars.Value >= 20 then -- Own 20 cars
					--[[
					if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124451069) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124451069)
					end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124451069)
				end
				if Cars.Value >= 30 then -- Own 30 cars
					--[[
					if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124451085) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124451085)
					end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124451085)
				end
				if Cars.Value >= 40 then -- Own 40 cars
					--[[
					if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124451097) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124451097)
					end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124451097)
				end
				if Cars.Value >= 50 then -- Own 50 cars
					--[[
					if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124465314) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124465314)
					end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124465314)
				end
			end)
			getCars.Level:GetPropertyChangedSignal("Value"):Connect(function()
				UpdateCarLevelEvent:FireClient(Player, getCars.Name, getCars.Level.Value)
				if getCars.Level.Value == 2 then -- Level 2
					--[[
				   if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124473926) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124473926)
				   end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124473926)
				end
				if getCars.Level.Value == 3 then -- Level 3
					--[[
				   if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124473927) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124473927)
				   end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124473927)
				end
				if getCars.Level.Value == 4 then -- Level 4
					--[[
				    if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124473928) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124473928)
					end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124473928)
				end
				if getCars.Level.Value == 5 then -- Level 5
					--[[
				    if not BadgeServiceModule:UserHasBadgeAsync(Player.UserId, 2124473930) then
						-- Award Badge
						BadgeServiceModule:AwardBadge(Player.UserId, 2124473930)
					end--]]
					BadgeServiceModule:AwardBadge(Player.UserId, 2124473930)
				end
			end)
		end
	end
end

Thanks for the reply!

You can just like

-- Add IdBadge
local Badges={
[10]=,
[20]=,
[30]=,
[40]=,
[50]=,
}
local function Award()
for i,v in Badges do
if Cars.Value >= i then
BadgeServiceModule:AwardBadge(Player.UserId, v)
end
end

--Connections... etc

You don’t need add many if, you can just create Dicitrations then add check if bigger or same then award that badge to player

(I don’t know if it’s gonna give only one or all at one time)

Thats pretty clean but i think its because of the overflow of calls which im not sure

These look like warnings and could happen if that part of the server is down.

crazy fix..
You’re asking if they have the badge then if not you’re awarding the badge. You could just award the badge and let a fail go silent in the pcall. Same outcome but keeps moving.

The issue is that you’re spamming the API every time the value of getCars changes. Instead, you could have a cache system per player, where as long the player stays in the same server, you don’t account for the situation where the delete a badge from their inventory, and just check the cache. Then, when they leave, you remove their cache, so if they rejoin they can get the badges they deleted.

The way I would implement the cache is with a dictionary with player keys, where each value is another dictionary of badgeId: isOwned pairs. I would implement a custom UserHasBadgeAsync function that uses this cache, and if it finds the result in the cache returns it, else it fetches it through the API and updates the cache. When a badge is awarded I would update the cache as well. The cache would be set to an empty array on player join and deleted on player leave. That way if a user wants their badges back, they can simply rejoin the server.

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