How to check if a player owns a gamepass?

hi, i want to check at the end of the game if the player has a gamepass (that doubles the coins that the player gets) and double the coins he gets if he owns the gamepass

if outcome == "time-up" then

		for i, c in pairs(game.Players:GetPlayers()) do

			if c:FindFirstChild("Contestant") then
				local player = c

				player.Tokens.Value = player.Tokens.Value + 20

				player:FindFirstChild("leaderstats").Survivals.Value = player:FindFirstChild("leaderstats").Survivals.Value + 1

				status.Value = "Time up"

			end
		end
end

any help is appreciated

2 Likes
1 Like

i read that, but how should i set it up? thank you

1 Like

You can check if a player has the badge in this part of the line

player.Tokens.Value = player.Tokens.Value + 20

by using :UserOwnsGamepassAsync(Player.UserId, GamepassId)

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 0000000) then
player.Tokens.Value = player.Tokens.Value + 20 * 2
else
player.Tokens.Value = player.Tokens.Value + 20
end

2 Likes
local MarketService = game:GetService("MarketplaceService")
local Players = game:GetService("Players") -- this is a service, use :GetService() to get services instead of game.Service

if outcome == "time-up" then
	status.Value = "Time up"
	for _, player in Player:GetPlayers() do
		if player:FindFirstChild("Contestant") then
			xpcall(function() -- xpcall in case something goes wrong
-- local player = c -- this part is not needed at all
				player.Tokens.Value += 20 * (if MarketService:UserOwnsGamePassAsync(player.UserId, GAMEPASS_ID_HERE) then 2 else 1)
				-- i'm doing a slightly fancy thing on top called a ternary operation. you don't need to know what that is yet, all you need to know is that it multiplies 20 by 2 if user has the gamepass. multiply 20 by 1 if they don't
				player.leaderstats.Survivals.Value += 1 -- yes, you can do number1 += number2 instead of number1 = number1 + number2.
			end, warn) -- instead of breaking the whole script, gives a warning if something errors
		end
	end
end
2 Likes

I didn’t even know you could use it like that. This could save a few couple of lines

(Your ternary operation)

yea
there’s a few ways to do it
the way i did is if condition then value1 else value2
there’s also condition and value1 or value2
but it can be a bit jank, and sometimes doesn’t work with strict typechecking so

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