Search code examples
luaroblox

How do i make 2x cash gamepass script Roblox


So I was making a script that gives you 5 cash every minute, I also made a game pass for the script if someone owns the game pass they get double money as the Non-game pass holders. Here is my script I Haven't any scripts to give cash but the problem is in the 2nd script block, the console print an error :

  09:10:57.466 ServerScriptService.CashGiver:6: attempt to index nil with 'UserId' - Server - CashGiver:6

local Give5Cash = game.ReplicatedStorage:WaitForChild("Give5Cash")
local Give10Cash = game.ReplicatedStorage:WaitForChild("Give10Cash")

Give5Cash.OnServerEvent:Connect(function()
    print("Player Will Be Given 5 Cash")
end)

Give10Cash.OnServerEvent:Connect(function()
    print("Player Will Be Given 10 Cash")
end)
while wait() do
    local MPS = game:GetService("MarketplaceService")
    local id = 16031172
    local player = game.Players.LocalPlayer

    if MPS:UserOwnsGamePassAsync(player.UserId, id) then
        game.ReplicatedStorage:WaitForChild("Give10Cash"):FireServer()
        print("Player Owns 2x Cash")
    else
        print("Players Doesnt Owns 2x Cash")
        game.ReplicatedStorage:WaitForChild("Give5Cash"):FireServer()
    end 
    
    wait(5)
end

Solution

  • So I figured out a way to solve this issue and I have got the answer for my question. Here is how I did it I made 3 scripts in ServerScriptService with the name of CashGiver5, CashGiver10, and CashGiverHandler here are the scripts I added to each script. CashGiver5:

    while wait(1) do
        print("Giving Player 5 Cash ")
        for i, player in pairs(game.Players:GetPlayers()) do
            player:WaitForChild("leaderstats").Cash.Value += 5
        end
    end
    

    CashGiver10:

    while wait(1) do
        print("Giving Player 10 Cash ")
        for i, player in pairs(game.Players:GetPlayers()) do
            player:WaitForChild("leaderstats").Cash.Value += 10
        end
    end
    

    CashGiverHandler:

    local MarketPlace = game:GetService("MarketplaceService")
    
    game.Players.PlayerAdded:Connect(function(player)
        local g = 16031172 -- DOUBLE CASH ID
        
        local Give5Script = game.ServerScriptService.CashGiver5
        local Give10Script = game.ServerScriptService.CashGiver10
    
        if MarketPlace:UserOwnsGamePassAsync(player.UserId, g) then
            Give5Script:Destroy()
        else
            Give10Script:Destroy()
        end
    end)
    

    WHAT THE SCRIPT DOES?

    So basically the CashGiver scripts are basic scripts giving player Cash every second. so the Handler script Destroys one of the scripts (s) when a player is added to the game.