Answered by
5 years ago Edited 5 years ago
Ok, there are a few things to address.
3 | local pla = game.Players:FindFirstChild(hit.Parent.Name) |
4 | game:GetService( "MarketplaceService" ):PromptPurchase(pla, 666 ) |
6 | script.Disabled = false |
Disabling the script after the event listener starts running won't actually prevent the event listener from running, as the others have suggested. The script still runs because it's an event listener; if the event occurs, a separate thread of code is created and runs regardless of the state of the Disabled
property of the parent script.
For anyone who is tempted to contest that, I tested this and it's true.
Okay, now that that's out of the way, the pla
variable isn't always going to be returning a player. It's entirely possible that a part that isn't from a player touches it.
Ensure that a player is touching the part:
2 | local pla = game.Players:FindFirstChild(hit.Parent.Name) |
5 | game:GetService( "MarketplaceService" ):PromptPurchase(pla, 666 ) |
7 | script.Disabled = false |
As you can see, I adjusted the code so that it only disables the script when a player touches the part.
Whenever the part is touched, it checks to see if a player touched the part, and if a player did touch the part, then prompt the purchase.
But another problem here is that you need to use :PromptGamePassPurchase()
and not :PromptPurchase()
.
I don't know if the two functions can be used interchangeably in the context of purchasing a game pass, but it's probably safer because there's a function that is delegated to the purchase of a game pass.
You also need to replace the 666 with the actual ID of your game pass, which can be found in the URL of the game pass. The script needs to know which particular game pass to prompt the player to purchase.
I know it's possible he put that there to conceal the game pass ID, but I'm just being safe.
04 | local pla = game.Players:FindFirstChild(hit.Parent.Name) |
06 | script.Disabled = true |
07 | game:GetService( "MarketplaceService" ):PromptGamePassPurchase(pla, ID) |
09 | script.Disabled = false |