Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I create a 2x cash Game Pass?

Asked by 4 years ago

Hello, I've been trying to experiment around with making a 2x cash game pass. I'm using Zednov's Tycoon kit. I'm not sure how to go about it, so I tried making a local script within the 2x Cash button with the following code snippet from this article:

https://developer.roblox.com/en-us/articles/Game-Passes-One-Time-Purchases

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

Here's a picture of my file hierarchy for clarity: https://imgur.com/a/qT8dLGM

This script is not working for me, whenever I step on the 2x cash button the option to buy the gamepass doesn't appear. What am I doing wrong, and what are the steps to solve this issue? I want to see the option to buy a gamepass appear. If you need more information don't be afraid to ask.

0
Don't. Micro-transactions are dumb. ArtsicleOfficial 171 — 4y
0
^ It helps the devs of games with them and why would they be dumb? If it was, they would have been removed by now mybituploads 304 — 4y
0
OP, your using a localscript (unless thats intended) Use a "Script" (blue one) mybituploads 304 — 4y
0
^ 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. Lorthran 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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,

  1. 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.

  2. 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

  1. 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)
  1. 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) -- We will then use the PlayerThatJoined variable and send it as the first argument in the promptPurchase function so the function can use it.
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)
0
Can you help me ? How do i exactly make a 2x Cash Button sorry i am New Scripter kiritousd -5 — 3y
Ad

Answer this question