Okay there a few problems with your script: 1. The script isn't even running 2. There are a few syntax errors 3. The function is never actually called so it doesn't run 4. The local player won't work in a server script
Lets get started,
The script is not running because it is a LocalScript. LocalScripts only run in certain places and your hierarchy clearly shows it is not in one of those places. (The LocalScript article on the wiki shows what places LocalScripts will run in.) Since you are probably going to issue the 2x cash from this script, change the LocalScript into a normal script and copy+paste the contents into it.
You simply messed up on writing the script a bit on Line 9 and Line 21 which will make the script error. On Line 9 you added an extra ")" and an end is needed on Line 21 to close the if statement started on Line 18. The only end there is closing the entire local function so this if statement has no end. The correct code is this:
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = 7465290
local function promptPurchase()
local player = Players.LocalPlayer
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass then
print("Yay!")
else
MarketplaceService:PromptGamePassPurchase(player, gamePassID)
end
end
- The local function promptPurchase is never actually called in the script meaning it doesn't run. (Think of it like this: If you installed a lamp it doesn't turn on automatically, you need to turn it on with the switch) So what we are gonna do is use the Players Service's PlayerAdded to detect when a player has joined the game and then call the function.
Players.PlayerAdded:Connect(function()
promptPurchase()
end)
- The LocalPlayer variable made on line 6 will no longer work because there is no LocalPlayer in a normal script so we need to change it. What we will do is use the PlayerAdded's first variable to get the actual player that joined. (All we have done up to this point was detect when a player joined, but haven't done anything with that player.)
Players.PlayerAdded:Connect(function(PlayerThatJoined)
promptPurchase(PlayerThatJoined)
end)
Now we can go into the function and add the PlayerThatJoined variable and switch the player variable to it:
local function promptPurchase(PlayerThatJoined)
local player = PlayerThatJoined
If you did everything right all players should now get prompted to purchase the gamepass when joining the game, or if they already own it then it will print "Yay!". If you need help on anything with this please send a comment on this answer and I will gladly help.
COMPLETE SCRIPT:
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = 7465290
local function promptPurchase(PlayerThatJoined)
local player = PlayerThatJoined
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass then
print("Yay!")
else
MarketplaceService:PromptGamePassPurchase(player, gamePassID)
end
end
Players.PlayerAdded:Connect(function(PlayerThatJoined)
promptPurchase(PlayerThatJoined)
end)
4 comments
Don't. Micro-transactions are dumb.
^ It helps the devs of games with them and why would they be dumb? If it was, they would have been removed by now
OP, your using a localscript (unless thats intended) Use a "Script" (blue one)
^ I tried changing it from a localscript to a Script, but that didn't change anything. I'm thinking that within the script I don't have it to where when you step on the button, it will show up.