I have a gamepass Item in my game the script I have that gives the item works fine but when the player who owns the pass dies the tool gets removed from their inventory here is the script:
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local boombox = game.ReplicatedStorage.Tools:WaitForChild('Boombox'):Clone()
local gamePassID = 45333610
local function onPlayerAdded(player)
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass == true then
print(player.Name .. " owns the game pass with ID " .. gamePassID)
boombox.Parent = player.Backpack
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
This is because you only check when the player is added, you should also check when the character is added.
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local boombox = game.ReplicatedStorage.Tools:WaitForChild('Boombox'):Clone()
local gamePassID = 45333610
local function onPlayerAdded(player)
local function CheckForPass()
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass == true then
print(player.Name .. " owns the game pass with ID " .. gamePassID)
boombox.Parent = player.Backpack
end
end
player.CharacterAdded:Connect(function(char)
CheckForPass()
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local boombox = game.ReplicatedStorage.Tools:WaitForChild('Boombox'):Clone()
local gamePassID = 45333610
Players.PlayerAdded:Connect(function(player)
local function CheckForPass()
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass == true then
print(player.Name .. " owns the game pass with ID " .. gamePassID)
boombox.Parent = player.Backpack
end
end
player.CharacterAdded:Connect(function(char)
CheckForPass()
end)
end)
Note: Make sure you always use RBXScriptSignals with non local functions attached: Player.PlayerAdded:Connect(function(...))
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local boombox = game.ReplicatedStorage.Tools:WaitForChild('Boombox'):Clone()
local gamePassID = 45333610
local function onPlayerAdded(player)
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass == true then
print(player.Name .. " owns the game pass with ID " .. gamePassID)
boombox.Parent = player.StarterGear
end
end
Players.PlayerAdded:Connect(onPlayerAdded)