I want to make this script give the badge to the first 5 people that buy my gamepass

Hello. I was attempting to make a badge script for a bonus car until I wanted it to give the badge to the first 5 people that bought one of my passes. Idk how cause I’m really bad at scripting
Script:

--edit these below bru
local badge = 000000 -- im not leaking my badges lol
local gamep = 000000 -- same with gamepasses

-- DO NOT TOUCH THESE BELOW UNLESS U KNOW WHAT U ARE DOING
local MS = game:GetService("MarketplaceService")
local plr = game.Players.

game.Players.PlayerAdded:Connect(function(plr)
	if MS:UserOwnsGamePassAsync(plr.UserId, gamep) then
		local b = game:GetService("BadgeService")
		b:AwardBadge(plr.userId, badge)
	end
end)

Dude, the plr variable is game.Players.

Why is their extra dot, remove it

Also, the name of the var plr and parameter is same. So it confuses which one to take, just either delete the var plr or rename it !

1 Like

You can use DataStoreService to store the number of people that bought your gamepass. Then, whenever a person buys a gamepass you would check the Data in the DataStore and if it is less than 5 you would award the badge.

1 Like

Hmmm, you need to use DataStore:GetSortedAsync for that though I guess.

2 Likes

Maybe try checking it’s productinfo. Idk what all stuff we can find in it. I’ll search documentation and then see. But if it does gives the first 5 people, then it would be a solution.

You can use ordered datastores to get the top 5 of people who bought your gamepass, then if a player joins and they are part of the top 5, give the badge.

Don’t forget to check if they own the gamepass then use :UpdateAsync() on the ordered datastore.

Also, you order them by the time they got the gamepass, like if person1 buys a gamepass before person2, the list would look like this.

{
    [1] = (user id of person1)
    [2] = (user id of person2)
}
1 Like

Yeah, that’s what I said, I hope it should work

You never mentioned anything about ordered datastores? You were also talking about product info.

Edit : Nevermind, it was in one of the other replies.

So what’s this ? I mentioned about it

Using DataStore:GetSortedAsync() returns a table like if someone purchased, save it in a data store and it will sort according to who purchased in those first 5 members and then award your badge.

1 Like

You cannot reliably know when a player bought a gamepass through the website. This is not something I would recommend trying, as gamepasses can be purchased through the website without being in-game.

A player could even buy the gamepass without having played your game in the first place.


With these two things in mind, how will you ever truly know which 5 players were the first to buy your gamepass? You can’t. Your only option is to use a developer product (which can only be purchased in-game).

1 Like

Or you could also use a table, make a table and save the players who bought your gamepasses and save the table in data store, then check the table and check the top 5 players.

Can’t they just rejoin though?

Edit : It’s hard to get the exact date someone bought a gamepass. I realized this after I read your reply 2 times.

1 Like

A player does not have to immediately join the game after purchasing a pass. So no, this is a moot point.

1 Like

But when they join, we can check if player has the particular gamepass and add in the list but ya, if they didn’t joined for long and someone else joined then even they bought the game pass firstly but won’t get their name.

Now the practical solution is using API either.

I already gave a sufficient solution to the problem (being a developer product), which only has the caveats of…

  1. Cannot be purchased outside the game.
  2. You must handle both the logic and storing of information for checking whether a player has already purchased it.

There’s really no other way to reliably check when exactly a player purchased a product, and I highly recommend using workspace:GetServerTimeNow() to sync purchase times.