I thought this was my best work to date but I had an unexpected side effect. I gave myself all the gamepasses rather than checking whether I did. I can’t see how to modify this to work properly.
The code works and prints that I have the game passes but I never bought them so I did something wrong.
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local gamePassIds = {
[192615743] = "EnergyGamePass",
[192623067] = "SpeedGamePass",
[192624779] = "ElementGamePass",
[192626500] = "ButtonPressesGamePass",
[192629919] = "TimeGamePass"
}
local function handleGamePassError(player, statName, errMsg)
warn("Error checking game pass ownership for "..statName.." : "..errMsg)
end
local function updatePlayerStats(player)
local success, errMsg = pcall(function()
if not player:IsDescendantOf(Players) then
return -- Skip processing if player is no longer in the game
end
local playerstats = player:FindFirstChild("playerstats")
if not playerstats then
return -- Skip processing if playerstats is missing
end
for gamePassId, statName in pairs(gamePassIds) do
local gamePassOwnershipSuccess, gamePassOwnershipError = pcall(function()
if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId) then
local statValue = playerstats:FindFirstChild(statName)
if statValue and statValue:IsA("NumberValue") then
statValue.Value = 2
end
print(player.Name.." owns the "..statName.." game pass.")
else
print(player.Name.." does not own the "..statName.." game pass.")
end
end)
if not gamePassOwnershipSuccess then
handleGamePassError(player, statName, gamePassOwnershipError)
end
end
end)
if not success then
warn("Error occurred for player "..player.Name..": "..errMsg)
end
end
Players.PlayerAdded:Connect(function(player)
updatePlayerStats(player)
end)
-- Loop through existing players using task.spawn
task.spawn(function()
for _, player in ipairs(Players:GetPlayers()) do
updatePlayerStats(player)
end
end)