When Player Leaves GamePass Tool Doesn't Save

Hi everyone, when player bought gamepass tool and leaves doesn’t save

Here is the code:

local ToolName = "Bottle"
local Players = game:GetService("Players")
local gamepassID = 115728942


MPS.PromptGamePassPurchaseFinished:Connect(function(player, gamepassID, purchased)
	if purchased then
				if gamepassID == 115728942 then
				
			script[ToolName]:Clone().Parent = player.Backpack
					script[ToolName]:Clone().Parent = player.StarterGear
					
		end		
	end
end)

2 Likes

Try creating a gamepasses folder in the Player object, sort of like a leaderstats folder but with a different name and make the values boolean. When a player buys a gamepass, make the boolean value true and when the player joins, check for the boolean value and give him the tool if he has the value to true. (Make sure to use a datastore to save the boolean values or it won’t save)

1 Like

Have you made an attempt to actually save the tool? Where are you “saving” the tool?
Also, don’t clone it to StarterGear, clone it under Player.Backpack or Character if you want them to equip it straight away.

1 Like

You’re only giving the tool to the player when they purchase the gamepass. This only happens once. On further occurrences you can just check and see if they own the gamepass

local MarketPlaceService = game:GetService("MarketPlaceService")
local function ownsgamepass(userid,gamepassid)
    local owns = pcall(MarketPlaceService.UserOwnsGamePassAsync,MarketPlaceService,userid,gamepassid)
    if owns  then
    --Give the player the tool
    end
   
end
1 Like

I just tried this but now it seems now that it doesn’t give you the tool and not save

here is the code:

MPS.PromptGamePassPurchaseFinished:Connect(function(player, gamepassID, purchased, userid)
	local owns = pcall(MPS.UserOwnsGamePassAsync,MPS,userid,gamepassID)
	if purchased then
				if owns then
			script[ToolName]:Clone().Parent = player.Backpack
			script[ToolName]:Clone().Parent = player.StarterGear
		end		
	end
end)

You need both functions:
PromptGamePassPurchaseFinished tells you to give players who just got the gamepass the items

UserOwnsGamePassAsync will tell you to give players who already own the gamepass the items

It looks like the issue with your code is that you’re not saving the player’s ownership of the gamepass anywhere. You need to use a datastore or a similar method to save whether or not the player has purchased the gamepass so that you can check for it when they rejoin the game.

Here’s an example of how you can use a datastore to save the player’s ownership of the gamepass:

local DataStoreService = game:GetService("DataStoreService")
local GamePassDataStore = DataStoreService:GetDataStore("GamePassOwnership")

local ToolName = "Bottle"
local Players = game:GetService("Players")
local gamepassID = 115728942

local function givePlayerTool(player)
script[ToolName]:Clone().Parent = player.Backpack
script[ToolName]:Clone().Parent = player.StarterGear
end

MPS.PromptGamePassPurchaseFinished:Connect(function(player, gamepassID, purchased)
if purchased then
if gamepassID == 115728942 then
-- Save the player's ownership of the gamepass
GamePassDataStore:SetAsync(tostring(player.UserId), true)
givePlayerTool(player)
end
end
end)

-- Check for the player's ownership of the gamepass when they join the game
game.Players.PlayerAdded:Connect(function(player)
local owned = GamePassDataStore:GetAsync(tostring(player.UserId))
if owned then
givePlayerTool(player)
end
end)

This code uses the DataStoreService to save the player’s ownership of the gamepass when they purchase it. Then, when the player joins the game, the code checks if they own the gamepass and gives them the tool if they do.

1 Like

I see.
Firstly add a Server-Script inside Server Script Service. (Don’t remove any code you have gotten)
and put this into the script.

local GamepassId = 000 -- PUT YOUR GAMEPASS ID HERE!!!!

local MarketPlaceService = game:GetService"MarketPlaceService"
game.Players.PlayerAdded:Connect(function(p)
	local succes =pcall(MarketPlaceService.UserOwnsGamePassAsync,MarketPlaceService,p.UserId,GamepassId)

	if succes then
		-- GIVE PLAYER TOOL
	end
end)
1 Like

Make sure to change the GamepassId variable to your gamepassId and lastly give the player the tool.

I have to mention that this not efficient Using DataStores is a bad practice rather than you could just check if the player owns gamepasses when they join the game and you are also using ChatGPT. Pretty sure it’s against the rules

it works!, but now I’m afraid what @lilmazen1234 typed about DataStore isn’t efficient, I will try his method too

Well I just tried it but doesn’t save when player leaves, @lilmazen1234

this is what I tried:

local RS = game:GetService("ReplicatedStorage")



local MarketPlaceService = game:GetService"MarketPlaceService"
game.Players.PlayerAdded:Connect(function(p)
	local succes =pcall(MarketPlaceService.UserOwnsGamePassAsync,MarketPlaceService,p.UserId,GamepassId)

	if succes then
		RS.Bottle:Clone().Parent = p.Backpack
		RS.Bottle:Clone().Parent = p.StarterGear
	end
end) 

Change the entire code to this:

local RS = game:GetService("ReplicatedStorage")
local MarketPlaceService = game:GetService("MarketPlaceService")

game.Players.PlayerAdded:Connect(function(p)
local PlayerBoughtThisGamePass =  MarketPlaceService.UserOwnsGamePassAsync(p.UserId, 115728942)

if PlayerBoughtThisGamePass then
  RS.Bottle:Clone().Parent = p.Backpack
RS.Bottle:Clone().Parent = p.StarterGear
end
end)
1 Like

it still doesn’t work, maybe I’m misunderstanding, should I add that code to the original one or the new one?, I added this code to the new one, sorry for this and thank you for your patience @lilmazen1234

1 Like

Is it a server script inside Server Script Service?

yes it is a server script inside the server script service

1 Like

I see the issue. My bad I’ve misspelled a letter.

This should work.

local RS = game:GetService("ReplicatedStorage")
local MarketPlaceService = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(p)
	local PlayerBoughtThisGamePass =  MarketPlaceService.UserOwnsGamePassAsync(p.UserId, 115728942)

	if PlayerBoughtThisGamePass then
		RS.Bottle:Clone().Parent = p.Backpack
		RS.Bottle:Clone().Parent = p.StarterGear
	end
end)
1 Like

Thank you for helping me and your patience :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.