Very Unreliable Gamepass Checker

Here is some background info about my game Frenzy Sprint: Basically each round you play, a random obstacle is selected for you to pass. If you lose, you are teleported to the lobby and play next time, with no rewards. If you win, you get 5 gold. There is a gamepass called Gold X2 that doubles the gold you earn when you complete a round (aka 5 Gold → 10 Gold). However, I’ve had quite a bit of trouble actually ascertaining whether or not the player actually has the pass.

So I have many stats under the player’s avatar (under a folder similar to leaderstats, but named differently) so that I can easily edit data the player has but may not need to directly know the stats of. One of them is how much Gold is added after each round is completed, called GoldToAddEachRound. I have it this way so that when the player’s character touches the Win button, their current balance is just added with GoldToAddEachRound.Value. If the player actually does possess the pass, no issues occur. However, if the player doesn’t, it seems to just randomly alternate between +5 or +10. I’ve been keeping track of the amount added each round, so I’m sure of this. It’ll be a seemingly random sequence, being something like this: 210, 215, 225, 235, 240, etc.

I have a script in ServerScriptService that creates these stats, another one that handles GoldToAddEachRound by checking if the player has the pass, and another in Workspace that adds it when the player touches the button. The one that checks if the player has the pass is the one that’s been giving me issues.

Here is that script that checks whether or not the player has the pass:

--I used things like hasGamepass2 because I have another inactive checker for a gamepass that's going to come out later on.
    local MarketplaceService = game:GetService("MarketplaceService")
    local Players = game:GetService("Players")

    local numToAdd = 5

    game.Players.PlayerAdded:Connect(function(player)
	
	local hasGamepass2 = false
	local gamepassId2 = 10118117
	
	local success2, message2 = pcall(function()
		hasGamepass2 = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId2)
	end)
	
	if not success2 then
		warn("Checking if player has gamepass" .. tostring(message2))
		return
	end
	
	if hasGamepass2 == true then
		if numToAdd == 5 then
		numToAdd = 10
	elseif numToAdd == 10 then
		numToAdd = 20
	end
end

    player.GoldToAddEachRound.Value = numToAdd
end)

Is there an issue with my gamepass checker? I would appreciate any and all help.

1 Like

From what I’m seeing here it looks like you’re checking for the game pass and setting the gold to 10 if the player owns it and 20 if the player doesn’t but only if they have 10.

Im not sure if you mean to do this but something like

if hasGamepass2 then
    numToAdd = 10
else
    numToAdd = 5
end

might suit your needs better.

Here is the fixed script:

--I used things like hasGamepass2 because I have another inactive checker for a gamepass that's going to come out later on.
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

 local numToAdd = 5

game.Players.PlayerAdded:Connect(function(player)
	
local gamepassId2 = 10118117
	
if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId2) then
    if numToAdd == 5 then
		numToAdd = 10
	elseif numToAdd == 10 then
		numToAdd = 20
	end
end

player.GoldToAddEachRound.Value = numToAdd

is this the full script because it is missing an end)

He didn’t give me the full script either. :man_shrugging:

well put an end) if that’s not the full script

Ok thanks everyone! Btw the end) was on the last line but the format got weird when I tried putting it into the DevForum format. That’s why the indentation is a little off.

You can mess around with this, it’s also a bit shorter, but very efficient.

local MarketplaceService = game:GetService('MarketplaceService')
local Players = game:GetService('Players')

local PassId = 000 -- Your ID here.

Players.PlayerAdded:Connect(function(Player)
	local Stats = Instance.new('Configuration')
	Stats.Name = 'leaderstats'
	Stats.Parent = Player
	
	local Gold = Instance.new('IntValue')
	Gold.Name = 'Gold'
    Gold.Value = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, PassId) and 50 or 10
	-- Inline expressions is very efficient.
	Gold.Parent = Stats
end)

Unfortunately none of the methods have been working. I’ve been using my test account, which doesn’t have the pass, but both my alt and main account keep getting the effects of the pass, with no distinguishing point. I’m honestly stumped because I’ve tried so many tutorials but nothing’s been working.

Are you testing this in studio or in-game?

In studio and in game. If you want to see it in action click on the link on my original post.

Works perfectly fine for me. Are you sure you have the right gamepass id?

Try this:

Gold.Value = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, PassId) and 999 or 111
print(Gold.Value)

It should print 999 if you own the gamepass, and 111 if you don’t.

1 Like

:man_facepalming: I didn’t, I feel so stupid right now ._. Thanks for pointing that out… I probably mixed up the two gamepasses.

1 Like