Reward coins ONCE through gamepass (not devproduct!, DataStore2)

  1. What do you want to achieve?

I have gamepasses called starter packs. Each will provide a few skins AND the most important thing: coins. I want the coins to be given once and never again kinda like a starter bonus.

  1. What is the issue?

I got something set up, but I realized that it isn’t the smartest idea. I let the script detect if the player owns the gamepass first of all, then if the player owns the badge given alongside the gamepass. If the player has the gamepass and not the badge yet, it will be rewarded with coins (player just bought the gamepass). Everything works perfectly fine, except the fact that you can delete badges from your inventory and repeat the process and get coins again and again.

I’d prefer to have a bool value inside the player which saves to determine whether they have received the reward yet or no.

It would be nice if one of you guys can combine it with DataStore2 as I am using this as my saving method!

local Players = game:GetService('Players')
local BadgeService = game:GetService('BadgeService')
local MarketPlaceService = game:GetService('MarketplaceService')

local GamepassId = id
local BadgeId = 788788663135547

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassId) then
			if not (BadgeService:UserHasBadgeAsync(player.UserId, BadgeId)) then
			
				local coinsDataStore = DataStore2("coins",player)
				coinsDataStore:Increment(5000)
				
				wait(2)
				
				BadgeService:AwardBadge(player.UserId, BadgeId)
				print("awarded")
			
			
			end
		end
	end)
end)

Any help appreciated!

2 Likes

I added a bool value inside the player called “Awarded1” which is set to false. I now need help with setting and saving it inside the script above

2 Likes

Anyone got an idea how to fix this?

1 Like

you can keep the track of rewarded coins in datastore. Here is a simple example

--consider ur plr data is structured something like this
local datatemplate = {
	Coins = 0,
	--other data
	
	--gamepass data
	Gamepasses = {
		SomeGamepass = {
			CoinsRewarded = false
		}
	}
}

local plrData = --get the player data table
--if the plr is still not rewarded coins after buying the gamepass 
if plrData.Gamepasses.SomeGamepass.CoinsRewarded == false  then
	--reward coins
	plrData.GamePasses.SomeGamepass.CoinsRewarded = true
	
	--save data
	
end

This can be done in a way more efficient way. the above example is somewhat logic of doing it

2 Likes