okey so i want to make something like 'touch here to buy game pass' and i search pop up gamepass script or brick and i found a script, but when i touch the brick it says the item is not currently available.
the script:
function onTouch(hit) script.Disabled=true local pla=game.Players:FindFirstChild(hit.Parent.Name) game:GetService("MarketplaceService"):PromptPurchase(pla, 666) --dont ask why 666 lol wait(1) script.Disabled=false end script.Parent.Touched:connect(onTouch)
Explain me what is wrong with that script
Ok, there are a few things to address.
function onTouch(hit) script.Disabled = true local pla = game.Players:FindFirstChild(hit.Parent.Name) game:GetService("MarketplaceService"):PromptPurchase(pla, 666) wait(1) script.Disabled = false end
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:
function onTouch(hit) local pla = game.Players:FindFirstChild(hit.Parent.Name) if pla then script.Disabled = true game:GetService("MarketplaceService"):PromptPurchase(pla, 666) wait(1) script.Disabled = false end end
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.
local ID = 123456789 -- Replace the numbers with the actual game pass ID function onTouch(hit) local pla = game.Players:FindFirstChild(hit.Parent.Name) if pla then script.Disabled = true game:GetService("MarketplaceService"):PromptGamePassPurchase(pla, ID) wait(1) script.Disabled = false end end
You made it so that the script gets disabled when an instance hits the part, which means that the script will get disabled and not run the code inside it. Maybe you were trying to make a cooldown/debounce? Also, I'd use :GetPlayerFromCharacter()
since it's a built-in function. The code below includes the cooldown/debounce.
local debounce = false local function onTouch(hit) if not debounce then debounce = true local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) game:GetService("MarketplaceService"):PromptGamePassPurchase(player, 666) wait(1) debounce = false end end script.Parent.Touched:Connect(onTouch)
Also, it probably says the item isn't available because it's off sale. Try using a gamepass that isn't off sale.