Why isn't it checking if the player owns the gamepass?

so I have a script that when you click a button it gives you 250 coins when you down own the pass, but 500 if you do. however, the script is failing to check if the player owns the gamepass. I have tried many different ways of doing this, and ive looked around a bit, but nothing has worked.

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local gamePassId = 678241646

	local success, productInfo = pcall(function()
		return game:GetService("MarketplaceService"):GetProductInfo(gamePassId)
	end)

	if success and productInfo then
		local hasGamepass = player:HasPass(gamePassId)

		local coinsToAdd
		if hasGamepass then
			coinsToAdd = 500
		else
			coinsToAdd = 250
		end

		local coins = player.leaderstats.Coins

		if coins then
			coins.Value = coins.Value + coinsToAdd
			print("Added", coinsToAdd, "coins to", player.Name)
		else
			print("Leaderstat 'Coins' not found for player:", player.Name)
		end
	else
		print("Failed to retrieve gamepass information for ID:", gamePassId)
	end
end)

Hello!

Are you receiving any errors? Also, I’m not sure if HasPass() is a valid method of Player. Have you tried using MarketPlaceService:UserOwnsGamepassAsync()?

I have, and it says “argument 1 is missing or nil”. for HasPass(), i get "attempted to index nil with “‘HasPass’”

I see. I recommend using UserOwnsGamepassAsync() again, though making sure you provide the necessary parameters.

UserOwnsGamepassAsync requires the following arguments:
1 - The User ID of the player you want to check.
2 - The Gamepass ID you want to see if the player owns.

Used correctly, it should look like this:
game:GetService("MarketplaceService"):UserOwnsGamepassAsync(player.UserId, gamePassId)

I also have another question. Is this script a LocalScript or is it on the server? If it’s a regular Server Script, then attempting to call :HasPass() would return the error as game.Players.LocalPlayer would be nil.

it’s a regular script. I just used UserOwnsGamepassAsync(player.UserId, gamePassId), and it gave an error saying “attempt to index nil with ‘UserId’”

Aha! So that is indeed the problem. LocalPlayer is a property that only exists on the client, so a regular Server Script won’t have any value for it. We’ll need to first get a variable for the player that interacts with your button.

On line 1, where the MouseButton1Click event is invoked, add a variable inside of function(). It should then look like this:

script.Parent.MouseButton1Click:Connect(function(player)

Then you can remove the statement for local player = game.Players.LocalPlayer. Now the error should be fixed!

I’d also recommend moving the pcall for GetProductInfo to the beginning of the script, outside of the event code, as it would unnecessarily run it every time the button is clicked.

First, I don’t think “player:HasPass(gamePassId)” is a function, do correct me if i’m wrong, but I’m almost 100% sure it isn’t.

I recommend using game:GetService(“MarketplaceService”):UserOwnsGamePassAsync(plr.UserId, passId) instead of whatever you used.

Try this.

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local gamePassId = 678241646

	if success and productInfo then
		local hasGamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, gamePassId)

		local coinsToAdd
		if hasGamepass then
			coinsToAdd = 500
		else
			coinsToAdd = 250
		end

		local coins = player.leaderstats.Coins

		if coins then
			coins.Value = coins.Value + coinsToAdd
			print("Added", coinsToAdd, "coins to", player.Name)
		else
			print("Leaderstat 'Coins' not found for player:", player.Name)
		end
	else
		print("Failed to retrieve gamepass information for ID:", gamePassId)
	end
end)

even when i put player as a variable in the function, i still get the error.

i have tried that too, it doesn’t work.

I see. Can you please let me know exactly what the button is? Is it a GuiButton, a ClickDetector, or something else?

Can you show us the Output to see any errors, blue lines etc?

it is a gui button

(30charlimit)

Maybe try this.

Ps: I’ve added comments explaining what things do.


script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer -- Gets the local player.
	local gamePassId = 678241646 -- This is the game pass.

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, gamePassId) then -- this will check if they have it. If they do, it will do the statement. If not, it will do the else statement.
player.leaderstats.Cash.Value += 500
else -- This is the else statement that will happen if the user does not own the game pass.
plr.leaderstats.Cash.Value += 250
end
end)

I do highly recommend firing a remote event to change the cash value, as using a local script to do so will only change it for the Client, and isn’t actually there.

the changes I made should indeed work.

If you experience issues, please let me know.

I also shortened it / simplified it, because the line where you were getting the product info was never used.

That line might’ve caused additional problems in the past or could’ve in the future as it wasn’t being used.

Personally, I find this uneccesary, and could cause confusion and unoptomization in code causing the game to lag as it’s running unecessary if else statements.

To sum it up, instead of doing the if hasGamepass then coinsToAdd = 500 etc function, you could instead do what I put in the new script which is just immeadiately changing it.

You also need to insert a LocalScript into the UI as it will not work otherwise.

Okay, I can better understand the problem now. The main error occurring here is that the event is being invoked on a Script and not a LocalScript. Because of this, we aren’t able to get the Instance of the player clicking your button.

What we should do is create a LocalScript with the event to run when the button is clicked, which we can then use game.Players.LocalPlayer for. Then we should fire a RemoteEvent to communicate to the server that someone clicked the button.

Then, as @King_GamingRobloxYT suggested, add a Script on the server with a function to run when the RemoteEvent is fired, and then do the calculation as to how to add the values.

ive put it into a server script, with a localscript activating a remote event in the button itself. it still errors with the marketplaceservice though, with the error of “attempt to index nil with ‘UserId’”

Is LocalPlayer being referenced in the server script or local script?

it is being referenced in a serverscript.

LocalPlayer doesn’t exist on the server. If you’re sending a remote event, the first argument passed should automatically be the player there and you can check it that way.

1 Like