Just to add to @b_oolean’s post, it’s a good practice to wrap calls like these in pcalls
since MarketplaceService:UserOwnsGamepassAsync()
makes a web call. Web calls have a chance to fail if the Roblox servers are down, the gamepass ID is invalid etc.
local MarketplaceService = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(player)
local Success, OwnsGamepass = pcall(function()
return MarketplaceService:UserOwnsGamepassAsync(player.UserId, GAMEPASS_ID)
end)
local Retries = 0
while not Success do
Retries = Retries + 1
if Retries == 5 then break end
Success, OwnsGamepass = pcall(function()
return MarketplaceService:UserOwnsGamepassAsync(player.UserId, GAMEPASS_ID)
end)
wait(1)
end
if not Success then
error(OwnsGamepass)
return
end
if OwnsGamepass then -- returned true
-- do stuff here
end -- no need for an else
end)