How do I check if a player has a particular badge or not?

I’m looking to write a script that checks if a player has a badge, and if not, then awards them. In this same script, I would also like to reward the player with a tool if they have the badge. I have the script I am working with, but I keep getting the “Unable to Cast Object” output statement.

How do I fix this?

----this is part of a much larger script, so anything not defined here is actually defined
local fifty_Kills = 2124489709 
local hundo_kills = 2124489710 
local gun1 = game.ServerStorage.Bonuses.Demonic:Clone()
local gun2 = game.ServerStorage.Bonuses.Valiant:Clone()
local tag = hum:findFirstChild("creator") 
if Leaderstats.Value >= 2 and not game:GetService("MarketplaceService"):PlayerOwnsAsset(tag.Value.userId, fifty_Kills) then
	game:GetService("BadgeService"):AwardBadge(tag.Value.userId, fifty_Kills)  
elseif Leaderstats.Value >= 3 and not game:GetService("MarketplaceService"):PlayerOwnsAsset(tag.Value.userId, hundo_kills) then
	game:GetService("BadgeService"):AwardBadge(tag.Value.userId, hundo_kills)   
	end
wait()
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player.userId, fifty_Kills) then
	gun1:Clone().Parent = tag.Value.Backpack
	gun1:Clone().Parent = tag.Value.StarterGear
elseif game:GetService("MarketplaceService"):PlayerOwnsAsset(player.userId, hundo_kills) then
	gun2:Clone().Parent = tag.Value.Backpack
	gun2:Clone().Parent = tag.Value.StarterGear  
end	
12 Likes

The function PlayerOwnsAsset takes a Player object, not a UserId. Simply change it to this:

----this is part of a much larger script, so anything not defined here is actually defined
local playerService = game:GetService("Players");
local fifty_Kills = 2124489709 
local hundo_kills = 2124489710 
local gun1 = game.ServerStorage.Bonuses.Demonic:Clone()
local gun2 = game.ServerStorage.Bonuses.Valiant:Clone()
local tag = hum:findFirstChild("creator") 
if Leaderstats.Value >= 2 and not game:GetService("MarketplaceService"):PlayerOwnsAsset(playerService:GetPlayerByUserId(tag.Value.userId), fifty_Kills) then
	game:GetService("BadgeService"):AwardBadge(tag.Value.userId, fifty_Kills)  
elseif Leaderstats.Value >= 3 and not game:GetService("MarketplaceService"):PlayerOwnsAsset(playerService:GetPlayerByUserId(tag.Value.userId), hundo_kills) then
	game:GetService("BadgeService"):AwardBadge(tag.Value.userId, hundo_kills)   
	end
wait()
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player, fifty_Kills) then
	gun1:Clone().Parent = tag.Value.Backpack
	gun1:Clone().Parent = tag.Value.StarterGear
elseif game:GetService("MarketplaceService"):PlayerOwnsAsset(player, hundo_kills) then
	gun2:Clone().Parent = tag.Value.Backpack
	gun2:Clone().Parent = tag.Value.StarterGear  
end	

Would game:GetService(“BadgeService”):UserHasBadgeAsync(player.userId, badgeid) work too? Someone on a server I’m in sent me an article on it, and I’ll be honest, I had no idea until five minutes ago.

3 Likes

To check if a player has a badge you should use BadgeService:UserHasBadgeAsync().
Example:

local BadgeService = game:GetService("BadgeService")

if BadgeService:UserHasBadgeAsync(UserId, BadgeId) then
    --Code 
end
37 Likes

If you’re trying to check if a user has a badge then yes this would work. Seems like most things that have to do with badges take a userid instead of a player since the player really doesn’t have to be in game. Type casting errors only occur when you plug in something that isn’t the same type as when the function uses it, or something in a similar situation. Languages that are “strongly typed” don’t usually have this problem because it usually warns you when try to set a variable of a different type, but lua does.