Home Garry's Mod Implementing a Virtual Currency System for In-Game Purchases on a Garry's Mod Server

Implementing a Virtual Currency System for In-Game Purchases on a Garry's Mod Server

Last updated on Feb 13, 2025

In this tutorial, you'll learn how to set up a virtual currency system on your Garry's Mod server. This system lets players convert real money into virtual credits which they can then spend on in-game items. We'll create a basic Lua script to track and manage player currency, laying the groundwork for integrating real money transactions. 🚀


📌 Prerequisites

  • A Garry's Mod server purchased at VolticHost.com

  • Access to the VolticHost Game Panel

  • Basic knowledge of Lua scripting and server file management

  • (Optional) A payment gateway setup for processing real money transactions


1️⃣ First Step - Creating the Virtual Currency Script

  1. Log in to VolticHost Game Panel.

  2. Navigate to the File Manager from the main dashboard.

  3. Go to the /garrysmod/lua/autorun/server/ directory.

  4. Create a new file named virtual_currency.lua.

  5. Open virtual_currency.lua for editing and paste the following script:

    if SERVER then
        util.AddNetworkString("CurrencyUpdate")
        local playerCurrency = {}
    
        hook.Add("PlayerInitialSpawn", "InitializeCurrency", function(ply)
            local steamID = ply:SteamID()
            playerCurrency[steamID] = playerCurrency[steamID] or 0
        end)
    
        concommand.Add("check_currency", function(ply)
            local steamID = ply:SteamID()
            local currency = playerCurrency[steamID] or 0
            ply:ChatPrint("Your current currency: $" .. currency)
        end)
    
        function AddCurrency(ply, amount)
            local steamID = ply:SteamID()
            playerCurrency[steamID] = (playerCurrency[steamID] or 0) + amount
            net.Start("CurrencyUpdate")
            net.WriteInt(playerCurrency[steamID], 32)
            net.Send(ply)
        end
    end
    
  6. Save the file after editing.

  7. Restart your server from the VolticHost Game Panel to load the new script.

Virtual currency script created and loaded successfully!


2️⃣ Next Step - Simulating Real Money Transactions

  1. In the same virtual_currency.lua file, add a command to simulate a purchase that credits virtual currency:

    concommand.Add("simulate_purchase", function(ply)
        local amount = 100 -- Simulated credit amount for a real money purchase
        AddCurrency(ply, amount)
        ply:ChatPrint("Purchase successful! $" .. amount .. " added to your account.")
    end)
    
  2. Save the changes to the file.

  3. Restart your server from the VolticHost Game Panel again to apply the update.

  4. Test the simulation by opening the in-game console and typing:

    simulate_purchase
    

    Then, check your currency with:

    check_currency
    

Virtual currency purchase simulation executed and verified successfully!


🎉 Conclusion

You have now successfully implemented a virtual currency system on your Garry's Mod server using the VolticHost Game Panel! This setup provides a foundation for integrating real money transactions, allowing players to convert their purchases into virtual credits for in-game use. 🚀

For more assistance, contact VolticHost Support.