Need help adding gamepasses with marketplaceservice

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)

1 Like

If you create a gamepass, then you own it, just like you bought it. If you want to check if gamepasses work, use Test Profile. (TEST > Test Profile > :white_check_mark: Enable Test Profile), I guess (I can’t access Studio rn)

Oh really, that’s cool. I don’t remember that being a thing last time I made a game. Is it a new feature? I guess I’ll have to test it somehow with a second player.

EDIT: you are correct - Test

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.