Help with UserHasBadgeAsync

  1. What do you want to achieve? Keep it simple and clear!
    I’m attempting to make a script that checks if the player has a specific badge from a set of id’s in a table and change a label accordingly.(keep In mind it’s checking a specific id from a set, that’s different for every label I want to change)

  2. What is the issue? Include screenshots / videos if possible!
    The main issue is when all the scripts to check if the user has the badge starts loading, some don’t return the UserHasBadgeAsync(I’m assuming it’s because of too many checks at once) leaving me with Processing: Screen Shot 2022-01-30 at 4.15.53 PM.png…

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I’ve tried putting it in a pcall and checking if it doesn’t return, to check again but it never gets it

script I’m using to check if the player has the badge:

local Player = game.Players.LocalPlayer

local BadgeService = game:GetService('BadgeService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

local StartValue = script.Parent.Parent.Parent.Parent.Main.CurrentNum
local Link = "rbxassetid://"
local Link2 = "   "

local BadgeName = script.Parent.TextLabel
local BadgeImage = script.Parent
local Status1 = BadgeImage.Status
local Module = ReplicatedStorage:WaitForChild('BadgeModule') -- modulescripts must be in replicatedstorage to connect with the client, remember that now
local Badges = require(Module)
local TotalBadge = #(Badges.ID)
local badge = 0
local function GetInfo(ID)
	if script.Parent.Parent.Parent.Parent.Main.CurrentNum.Value <= TotalBadge then
		BadgeImage.Image = Link .. BadgeService:GetBadgeInfoAsync(Badges.ID[script.Parent.Parent.Parent.Parent.Main.CurrentNum.Value+129]).IconImageId
else
	BadgeImage.Visible = false
	end
	if BadgeImage.Visible == true then
		local success, badgeInfo = pcall(function()
			return BadgeService:UserHasBadgeAsync(Player.UserId,Badges.ID[script.Parent.Parent.Parent.Parent.Main.CurrentNum.Value+129]) 
				
		
		end)
if success then
			if badgeInfo then
				BadgeImage.Status.Text = '✅'
		   else
			BadgeImage.Status.Text = '❌'
			end
					
		end	
	if not success then
			BadgeImage.Status.Text = '?'
			print("unsucessful")
		end
		while not success do
			GetInfo(Badges.ID[StartValue.Value])
		end
	end
	end

		
	if BadgeImage.Visible == true then
		BadgeName.Text = BadgeService:GetBadgeInfoAsync(Badges.ID[script.Parent.Parent.Parent.Parent.Main.CurrentNum.Value+129]).Name
	end


GetInfo(Badges.ID[StartValue.Value])
local mouse = Player:GetMouse()
script.Parent.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		badge = 1
	end

end)
script.Parent.InputEnded:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		badge = 0
	end
end)
	

	
	

while true do
	wait()
	
	if badge == 1 then
	script.Parent.Parent.Parent.Parent.Parent.Hint.Visible = true
	script.Parent.Parent.Parent.Parent.Parent.Hint.Position = UDim2.new(0,mouse.X,0,mouse.Y)
	script.Parent.Parent.Parent.Parent.Parent.Hint.Frame.TextLabel.Text = Link2 .. BadgeService:GetBadgeInfoAsync(Badges.ID[script.Parent.Parent.Parent.Parent.Main.CurrentNum.Value+129]).Description
	end
	
	if badge == 0 then
		script.Parent.Parent.Parent.Parent.Parent.Hint.Visible = false
	script.Parent.Parent.Parent.Parent.Parent.Hint.Position = UDim2.new(0,0,0,0)
	script.Parent.Parent.Parent.Parent.Parent.Hint.Frame.TextLabel.Text = ""
	end
	
	
end


what it looks like when some don’t load:

Uploading: Screen Shot 2022-01-30 at 4.21.06 PM.png…

This is also my first post ever on the forum, so if it’s lacking in detail or if you want to know something else, let me know. Also why I’m following the script support holy grail formula.

1 Like

looks like the images haven’t uploaded, but it’s leaving me with a UserHasBadgeAsync: failed due to empty response error

1 Like

You can’t call UserHasBadgeAsync from a local script. You should change check on the server and fire a remote event to the client to change the text.

that seems like a possible solution, but then why are the other ones working(if it couldn’t be called from a script, why do some of the labels appear as a :white_check_mark: or​:x: when I’m testing in studio).

Also wouldn’t a remote event take it even longer to check?

It should not make a huge difference in time if you use remote events. And I do not know why the labels are showing checkmarks sometimes but not for others

So I found a few things including, unfortunately why @sau2000’s solution isn’t applicable for me in this case.

So first, I should have clarified what result I wanted more clearly. I’m trying to use UserHasBadgeAsync in a local script to check a specific ID from a set of tables. Then if it returns the set the text to a checkmark or X. I’m doing this for multiple different text’s with different local script’s in each to change the text because I need to check for more than 1 ID. The main reason the remote event solution doesn’t work is because I need to check for more than one ID.

If I have a script in ServerScriptService, checking one specific ID and then firing a remote to the local script to then change the text accordingly for that ID, fine.
Now say I want to do that for 10 ID’s. Now I need at least 10 different remote events to check those ID’s, and send to the local scripts, because they’re all checking different ID’s for the text.

Now say I have over 160 badges to check. I know it’s technically possible, but it would be pretty annoying to have 160 different remote events anyway. So unless there’s a way to do this with less remote events that I didn’t think of this will not work for me.

Secondly, that fact that only about half of the scripts I’m currently using give me the UserHasBadgeAsync: failed due to empty response error implies some of them did the check successfully. For me, this makes sense due to this post where the solution says you can use LocalPlayer, which is what I’m doing.

So I guess the real question I’m asking is, how due I make the check return consistently for all of them using LocalPlayer.

Or you could just perform the 160 checks simultaneously and then return the results back to the client. One RemoteEvent instance & two requests required, one from the client to the server (with the badge IDs to get info for) and one from the server back to the client (returning the retrieved data for those badge IDs).

Table values are what you need.

1 Like

So basically,

  1. The badge ID I want for that specific script and label is sent to the server script from the local script
  2. When it’s sent check it and send the result back to the client
  3. change text depending on the result

Also what are table values?

I tried with remote events anyway for one badge ID and it seems to have failed the same way because my pcall keeps on checking forever.

local badgeIds = {1, 2, 3, 4, 5}

With tables you can pack multiple values into a single one (like in the above example).

Yeah but how would I match the correct badge ID data from the server to the correct text. Would the returned data from the server be return into a script that then sorts them to the correct text I need to change(because I need to change multiple)?

Use UserHasBadge instead which isn’t asynchronous and can run from the client.

As stated before I’m 99.9% convinced at least for my purpose’s that I don’t need a server sided script to get the result I want from the label. My question was why half of them seemed to work with the localscripts I had while the others didn’t. I also did try using a remote event(and UserHasBadge with a localscript) that checked on a server script and I had the same result.

This video shows what I mean regarding local scripts working for my case:

the script I used for it

 local BadgeService = game:GetService("BadgeService")
local BadgeId = 2124860208
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local modul = ReplicatedStorage:WaitForChild('BadgeModule')
local module = require(modul)
local player = game:GetService("Players").LocalPlayer
local TotalBadge = #(module.ID)

-- Fetch badge information


for i, v in pairs(module.ID) do

local success, result = pcall(function()
		wait(0.1)
		return BadgeService:GetBadgeInfoAsync(v)  -- Change this to desired badge ID
		
end)

local function badgename()
if BadgeService:UserHasBadgeAsync(player.UserId, v) then
	
print("The user has this", result.Name)
else
		print("The user does not have")
	end
	end
	if success then
		wait(0.1)
	print("sucess")
	badgename()
end
if not success then 
	warn("badge check failed due to empty response")
end
end 


I’m wondering why specifically only the UserHasBadgeAsync check is failing especially when the one to grab the badge information is working as you can see from this screenshot:
Screen Shot 2022-02-01

the reason the one on the side doesn’t show the text is because it has to check UserHasBadge first and it failed, I removed that part of the code for the other one to show my point

So, my noobiness made me see all the UserHasBadgeAsync has failed due to empty response, but not what came before it.

Because I had the TooManyRequests error the scripts that tried to check after it failed because they didn’t know to stop after it failed, thus why I got the empty response error. My problem seems to be very similar to this post and hopefully my solution will be to.

1 Like