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)
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)
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.
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
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)
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.
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)
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
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)
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)
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
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)