Ghost errors with Gamepass Team Select

Heya! I’ve been working on my game for a while, and I’m trying to make a gamepass team select.
However, there’s a problem. I keep getting ghost errors, and I’m not sure what I did wrong as output yields to no avail.

game:GetService("Teams")
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 9788905)
	then player.TeamColor = "Burnt Sienna"
		player:LoadCharacter()
		if game:GetService("MarketplaceService"):UserOwnsGamepassAsync(player.userId, 9788905) == false
		then
			game:GetService("MarketplaceService"):PromptProductPurchase(player.userId, 978905)
			if game:GetService("MarketplaceService"):PromptProductPurchaseFinished() then
				if game:GetService("MarketplaceService"):UserOwnsGamepassAsync(player.userId, 9788905) then
					player.TeamColor = "Burnt Sienna"
					player:LoadCharacter()
				end
			end
		end
		
	end
end)

Any help would be appreciated!

This script makes no sense. You called an if statement on the player owning gamepass 9788905, and then called :PromptProductPurchase() rather than a gamepass. And then you tried to connect to the .PromptProductPurchaseFinished event by using it in an if statement and also using it as a method rather than an event (you used : not .). Then, within that same event connection you call an if statement on the user owning the gamepass 9788905 again.

So long story short, the reason that this script doesn’t work is because it makes no sense. You should figure out whetherthis is a dev product or a gamepass (I assume it’s a gamepass but can’t tell in your script), and then use the Dev Hub to learn how to handle purchases/check for ownership accordingly.

1 Like

Okay damn my reply was so mean, I will offer more help in this one. All you need to do to make this script work is the following:

  1. Check if the user owns the gamepass 9788905. If they do, then connect to the .MouseButton1Down() event that you made which allows them to change their team.
  2. Next, connect to the .PromptGamepassPurchaseFinished() event, and if the user ends up purchasing gamepass 9788905 (you can check this because both the gamepass ID and whether the user purchased it are passed as parameters), then you should also connect to the .MouseButton1Down() event you created.

That’s all (:

1 Like

Bit confused, do you mind elaborating? Sorry for the late reply, I was tinkering with the script in a test game.

What is it that you are confused about?

1 Like

I’m confused on how to implement your solution into my one, I don’t really understand what goes where.
Sorry, I’m still a pretty big newbie at scripting.

Upon looking at the script again, I realize that a lot of what you’re trying to do is not possible with your framework:

You are using a local script for handling clicks (as you should), but you have to change a player’s team and do :LoadCharacter() with a server script. Therefore, your local script should be connected to a remote event which will complete the task. Do the following:

In your local script, make a variable “OwnsPass” and set it to false. Then, if the player either owns the gamepass already (UserOwnsGamePassAsync()) or they buy it in the game (promptGamePassPurchaseFinished()), set OwnsPass to true. Whenever the user clicks on the Button, if the own the pass, a RemoteEvent will fire to the server which will call :LoadCharacter() on the player and also change their team. If OwnsPass is false, simply have the local script prompt a purchase to the user to buy the gamepass.

If the above doesn’t make sense to you, please do research on RemoteEvents, UserOwnsGamePassAsync(), and PromptGamePassPurchaseFinished(), because otherwise you simply aren’t going to be able to make scripts like these.

1 Like

Alright, I understand everything, I guess I’ll just need to research RemoteEvents. Thanks for the help!

1 Like

Here’s the new script.

local player = game.Players.LocalPlayer
local OwnsGame = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Click:Connect(function()
 	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 9788905)
	then OwnsGame = true
		ReplicatedStorage.GameTrue:FireClient()
		if OwnsGame == false then
			game:GetService("MarketplaceService"):PromptProductPurchase(player.userId, 978905)
			if game:GetService("MarketplaceService"):PromptProductPurchaseFinished() then
				OwnsGame = true
				ReplicatedStorage.GameTrue:FireClient()
			end
		end
	end
end)

Is this right?

Well, no. Lots of issues. Like, the entire script is just ordered completely differently than how I recommended (and will also not work since it is not ordered properly). Refer to this to structure your scripts correctly.

1 Like

Like this?

local player = game.Players.LocalPlayer
local OwnsPass = false
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

script.Parent.MouseButton1Click:Connect(function()
	 	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 9788905) or
				game:GetService("MarketplaceService"):PromptProductPurchaseFinished()
		
	then OwnsPass = true
	end
end)

No, that’s not at all how you use those events. Please view the DevHub’s articles on these topics before continuing further.