Make Gamepass Tool Stay In Inventory

Hi ive made a shop where players can purchase a speed coil (a game pass) but when they die it disappears out of their inventory. Preferably i’d like it to keep in their inventory even when they leave and re join the game. Ive tried various codes but cant find anything to do this.

local MarketPlaceService = game:GetService("MarketplaceService")

local Players = game:GetService("Players")

local speedcoilpassID = 85157369

game.Players.PlayerAdded:Connect(function(player)
	
	local success, message = pcall(function()
	haspass = MarketPlaceService:UserOwnsGamePassAsync(player.UserId, speedcoilpassID)
end)

if haspass then
	
	print("player has the speedcoil")
	
	
	local speedcoil = game.ReplicatedStorage.SpeedCoil:Clone()
	speedcoil.Parent = player.Backpack
	end
	
end)

local function onPromptGamePassPurchaseFinished(player, purchasedpassID, purchaseSuccess)
	
	if purchaseSuccess == true and purchasedpassID == speedcoilpassID then
		print(player.Name.."purchased the speed coil!")

		local speedcoil = game.ReplicatedStorage.SpeedCoil:Clone()
		speedcoil.Parent = player.Backpack
		
	end
end


MarketPlaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)

Heres my code. Purchasing and receiving the tool is fine. Just when they die or leave they lose it.

Anyone know how to help?

3 Likes

Add the same event to Player.CharacterAdded function it runs whenever player respawns

Reference player and every time when character is added do same stuff same but on character.added, so its fired every time player spawns

local MarketplaceService = game:GetService('MarketplaceService')
local Players = game:GetService('Players')
local Gamepass = 85157369

Players.PlayerAdded:Connect(function(Player)
    local Success, HasPass = pcall(function()
        return MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Gamepass)
    end)

    Player.CharacterAdded:Connect(function(Character)
        if HasPass then
            game:GetService('ReplicatedStorage').SpeedCoil:Clone().Parent = Player.Backpack
        end
    end)
end)

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, GamepassId, Purchased)
    if Player ~=  nil and GamepassId == Gamepass and Purchased then
        game:GetService('ReplicatedStorage').SpeedCoil:Clone().Parent = Player.Backpack
    end
end)

Enjoy! :smiley:

So find the player, access “their” datastore, so when they purchase the game pass tool it will save there then if they join in the future or leave it will save them, incase they’ve maybe deleted it from their purchases and such.
Or use marketplaceservice to see if they’ve bought it yeah.

i used this but it still deleted from inventory after i died

You’re missing player.CharacterAdded AND using the pcall wrong.

Here:

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

local Players = game:GetService("Players")

local speedcoilpassID = 85157369

Players.PlayerAdded:Connect(function(player)

    player.CharacterAdded:Connect(function(character) -- This was needed

      local success, hasPass = pcall(function() -- Changing the pcall to the correct format
	   return MarketPlaceService:UserOwnsGamePassAsync(player.UserId, speedcoilpassID)
      end)

      if success then -- Checking if successful
          if hasPass then -- Checking if the player has the gamepass
              local speedCoil = ReplicatedStorage.SpeedCoil:Clone()
              speedCoil.Parent = player.Backpack
          end
      end

    end)
	
  end)
	
end)

local function onPromptGamePassPurchaseFinished(player, purchasedpassID, purchaseSuccess)
	
	if purchaseSuccess == true and purchasedpassID == speedcoilpassID then
		print(player.Name.."purchased the speed coil!")

		local speedcoil = game.ReplicatedStorage.SpeedCoil:Clone()
		speedcoil.Parent = player.Backpack
		
	end
end


MarketPlaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)
1 Like

You need to check if the pcall is successful with if Success then. That’s the whole point of the pcall in the first place…

used this also and it still doesnt work. When i join my game it doesnt give me the speed coil

Are there any errors in the output?

I just tried the script and it workes fine.

ye it works now. I had 2 ‘Ends’ n it broke it XD

2 Likes