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