Gamepass - issue

  1. What do you want to achieve? Keep it simple and clear!
    When a player buy the gamepass, it would give him 4500 Coins in-game currency only 1 time. ( It would be a benefit being a “VIP”, that’s why I don’t use a DevProduct).
  2. What is the issue? Include screenshots / videos if possible!
    The script keeps giving the player 4500 Coins everytime they rejoin the game.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to save the award with datastore. Not sure where it went wrong. I tried a few solutions from the Devforum, but none worked in my case.

Below is the script that I use.

local settings = require(workspace.Imaginary.Configurations)
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore(settings.datastoreName)
local award = 4500
local PassId = 26550620

game:GetService("Players").PlayerAdded:connect(function(Player)
	local AwardsThePlayer = Player:WaitForChild("RandomStats"):WaitForChild("AwardsThePlayer")  			
	if AwardsThePlayer.Value == true then 
		script:Destroy()
	end
	if game:GetService("GamePassService"):PlayerHasPass(Player, PassId) then
		AwardsThePlayer.Value = true 		
		Player:WaitForChild("RandomStats"):WaitForChild("Coins").Value += award
		SaveDataStore:SetAsync(Player.UserId, AwardsThePlayer.Value)
end
end)

AwardsThePlayer is a BoolValue, original value is set to False.

I’d recommend referencing all of your Services at the start of your script so that you don’t have to keep getting them every time a Player joins the game, if you just want to check if that specific Player has a Gamepass and give them Coins once, yes you can use a Datastore but you’ll have to use pcall functions to properly save the Data and call SetAsync/GetAsync at the appropriate times:

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local MPS = game:GetService("MarketplaceService")

local settings = require(workspace.Imaginary.Configurations)
local SaveDataStore = DSS:GetDataStore(settings.datastoreName)
local AwardAmount = 4500
local GamepassID = 26550620

Players.PlayerAdded:Connect(function(Plr)
    local RandomStats = Player:WaitForChild("RandomStats")
    local AwardBool = RandomStats:WaitForChild("AwardsThePlayer")
    local Coins = RandomStats:WaitForChild("Coins")

    local CheckData 
    local success, whoops = pcall(function()
        CheckData = SaveDataStore:GetAsync(Plr.UserId)
    end)
    
    if success and CheckData then
        print("Data loaded,", Plr, "already owns the gamepass!")
        return

    elseif MPS:UserOwnsGamePassAsync(Plr.UserId, GamepassID) then
        AwardBool.Value = true
        Coins.Value += AwardAmount
        local success, whoops = pcall(function()
            SaveDataStore:SetAsync(Plr.UserId, AwardBool.Value)
        end)

        if success then
            print("Successfully saved the data of Player", Plr.Name)
        else
           warn("Error occured:", whoops)
        end
        
    end
end)

And make sure that you have API Services turned on in your Game Settings as well

1 Like

Hello, Jack!
Thank you for your info! It helped me to achieve my goal. I’ve learnt something new about the DS today.

2 Likes