Shirt buy Badge

can someone help me write a script so that when a player buys a shirt and he has it, a badge is issued specifically for the player who bought the shirt.

when you say “he has it” do you mean if they are wearing it or just own it?

Sorry for not specifying, yes own it

im not exactly sure how to see if someone owns the shirt, but if they spawn in with it on you can just give them the badge when they spawn, or if they buy it from something in your game you can give them the badge as they purchase it. im not sure how to check if they actually own the shirt if they dont do either of those though. maybe add a sign saying to join the game with the shirt on to get the badge?

local badge = 0;
local shirt = “”;

game.Players.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait();

if char:FindFirstChildOfClass(“Shirt”) and char:FindFirstChildOfClass(“Shirt”).ShirtTemplate == shirt then
print(plr.Name, “is wearing the shirt”);

  if game.BadgeService:UserHasBadgeAsync(plr.UserId, badge) then
  	print(plr.Name, "already has the badge");
  else
  	local success, err = pcall(function()
  		game.BadgeService:AwardBadge(plr.UserId, badge);
  	end);
  	
  	if success then
  		print(plr.Name, "has been awarded the badge");
  	else
  		warn("Error while awarding the badge:", err);
  	end
  end;

else
print(plr.Name, “is not wearing the shirt”);
end;
end);

this should work

its put on serverscriptservice?

Yes, you put it in serverscriptservice

Hello!
So couple of things.

This only works if they are wearing the shirt, not if they own it.

How to actually tell if someone owns a shirt? Give me one second Im cooking it up rq

this script doesnt work (aaaaaaaaaaaa)

It’s possible that the clothes will be on, I don’t care

right

local badge = 0;
local shirt = “”;

game.Players.PlayerAdded:Connect(function(plr)
if game.MarketplaceService:PlayerOwnsAsset(plr, shirt) then
print(plr.Name, “owns the shirt”);

	if game.BadgeService:UserHasBadgeAsync(plr.UserId, badge) then
		print(plr.Name, "already has the badge");
	else
		local success, err = pcall(function()
			game.BadgeService:AwardBadge(plr.UserId, badge);
		end);

		if success then
			print(plr.Name, "has been awarded the badge");
		else
			warn("Error while awarding the badge:", err);
		end
	end;
else
	print(plr.Name, "does not own the shirt");
end

end);

this should be fine

the new one should work only using the id

-- // Services //
local Marketplace = game:GetService('MarketplaceService')
local BadgeService = game:GetService('BadgeService')

-- // Ids //
local ShirtId = 0000 -- Replace with your shirt ID
local BadgeId = 0000 -- Replace with your Badge ID

-- // Functions //
local function PlayerOwnsShirt(Player:Player, Shirt:number) : boolean
	local Owned = false
	
	local success, errMsg = pcall(function() -- We use a pcall in the case of an error, that way code doesnt break!
		Owned = Marketplace:PlayerOwnsAsset(Player,Shirt) -- We use marketplace service so we can tell if they own a shirt
	end)
	
	if success then
		return Owned -- We use return so scripts can tell if it worked or not!
	else
		warn(errMsg)
		return false
	end
end

local function AwardBadge(Player:Player,Badge:number) : boolean -- || Gives a badge to the Player ||
	local success, errMsg = pcall(function() -- We use a pcall in the case of an error, that way code doesnt break!
		
		if not BadgeService:UserHasBadgeAsync(Player.UserId,Badge) then -- This checks if they own the badge, if not, then we can award it
			BadgeService:AwardBadge(Player.UserId,Badge) -- Awards the badge
		end
	end)
	
	if not success then
		warn(errMsg)
		return false
	else
		return true
	end
end


-- // You would then do this whenever you want to check if the player has a the shirt: //

local Player:Player = '' -- Replace with the actual player

if PlayerOwnsShirt(Player,ShirtId) then -- This would check if the player has the shirt
	AwardBadge(Player,BadgeId) -- if they have the shirt reward it
end

Note: that you cant be rewarded a badge inside of roblox studio, so to test this you would need to go into the game.
Put this script in: ServerScriptService (or any place that scripts can run)
You can use the code at the bottom to award the badge when needed!

Also please read the comments as it tells you how to do stuff :]

@DanilKolikYT

yes this script works (asijfkjasfkjakjsf)

im try this too (adasjidjkasd)

This badge is issued even when the person is not wearing a T-shirt?

Yup! As long as they own the Tshirt they will be awarded the badge! (That assumes you use the functions N stuff at the right time, for instance add this in your code)

-- // Services //
local Marketplace = game:GetService('MarketplaceService')
local BadgeService = game:GetService('BadgeService')

-- // Ids //
local ShirtId = 0000 -- Replace with your shirt ID
local BadgeId = 0000 -- Replace with your Badge ID

-- // Functions //
local function PlayerOwnsShirt(Player:Player, Shirt:number) : boolean
	local Owned = false
	
	local success, errMsg = pcall(function() -- We use a pcall in the case of an error, that way code doesnt break!
		Owned = Marketplace:PlayerOwnsAsset(Player,Shirt) -- We use marketplace service so we can tell if they own a shirt
	end)
	
	if success then
		return Owned -- We use return so scripts can tell if it worked or not!
	else
		warn(errMsg)
		return false
	end
end

local function AwardBadge(Player:Player,Badge:number) : boolean -- || Gives a badge to the Player ||
	local success, errMsg = pcall(function() -- We use a pcall in the case of an error, that way code doesnt break!
		
		if not BadgeService:UserHasBadgeAsync(Player.UserId,Badge) then -- This checks if they own the badge, if not, then we can award it
			BadgeService:AwardBadge(Player.UserId,Badge) -- Awards the badge
		end
	end)
	
	if not success then
		warn(errMsg)
		return false
	else
		return true
	end
end


-- // You would then do this whenever you want to check if the player has a the shirt: //

game.Players.PlayerAdded:Connect(function(player)
    if PlayerOwnsShirt(player,ShirtId ) then AwardBadge(player,BadgeId) end -- if player owns shirt, then reward it
end
1 Like

this badge give to player who buy shirt, yea?

Do you have you have code in your game that prompts the player to buy a shirt? As of now how I have the code it only runs when the player joins, you’d need to call these functions when the player buys the shirt

all thanks the script works as it should!

1 Like