BadgeService:UserHasBadgeAsync() not working

I want to detect if a player has a badge but it’s isn’t working, here is the part of the code where it not working:

    if success then
        cash.Value = ds:GetAsync(plr.UserId .. "-Cash")
        shard.Value = shard:GetAsync(plr.UserId .. "-Shards")
        xp.Value = xp:GetAsync(plr.UserId .. "-XP")
        level.Value = level:GetAsync(plr.UserId .. "-Levels")
        
        if BadgeService:UserHasBadgeAsync(plr.UserId, 2144450000) == false then -- there
            cash.Value = 500; shard.Value = 0; xp.Value = 0; level.Value = 1
            
            task.wait(1)
            BadgeService:AwardBadge(plr.UserId, 2144450000)
            if canGiveBetaBadge == true then BadgeService:AwardBadge(plr.UserId, 2144485549); end
            
        end
        
    else warn(errormessage); end

whole script:


can anybody help?

do you have any errors in the output?

Please put your badge stuff in a pcall function and tell me if you get any errors. When using the badge service you should really use a pcall function.

I have just tried this and it works fine (prints false).

1 Like

Hi, I did what you said, but it’s doesn’t work, here is the code

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("MondStats") 
local BadgeService = game:GetService("BadgeService")
local canGiveBetaBadge = true

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", folder)
	cash.Name = "Cash" 
	
	local shard = Instance.new("IntValue", folder)
	shard.Name = "Shards" 
	
	local xp = Instance.new("IntValue", folder)
	xp.Name = "XP" 
	
	local level = Instance.new("IntValue", folder)
	level.Name = "Levels" 
	
	local data 
	local success, errormessage = pcall(function()
		data = ds:GetAsync(plr.UserId)
	end)

	if success then
		cash.Value = ds:GetAsync(plr.UserId .. "-Cash")
		shard.Value = shard:GetAsync(plr.UserId .. "-Shards")
		xp.Value = xp:GetAsync(plr.UserId .. "-XP")
		level.Value = level:GetAsync(plr.UserId .. "-Levels")
		local badgeData
		local badgeSucess, badgeErrorMessage = pcall(function()
			badgeData = BadgeService:UserHasBadgeAsync(plr.UserId, 2144450000)
		end)
		if success then
			if badgeData == false then
				cash.Value = 500; shard.Value = 0; xp.Value = 0; level.Value = 1
			
				BadgeService:AwardBadge(plr.UserId, 2144450000)
				if canGiveBetaBadge == true then BadgeService:AwardBadge(plr.UserId, 2144485549); end
				
			end
		end
		
	else warn(errormessage); end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		ds:SetAsync(plr.UserId .. "-Cash", plr.DS.Cash.Value)
		ds:SetAsync(plr.UserId .. "-Shards", plr.DS.Shards.Value)
		ds:SetAsync(plr.UserId .. "-XP", plr.DS.XP.Value)
		ds:SetAsync(plr.UserId .. "-Levels", plr.Levels.Cash.Value)
		
	end)
	
end)

What issue do you have? It just does not work or what? Any errors in the output?

founded the error, im just brainless but thanks for helping me!
image

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