Home Garry's Mod Creating a Virtual Item Shop on a Garry's Mod Server

Creating a Virtual Item Shop on a Garry's Mod Server

Last updated on Feb 13, 2025

In this tutorial, you'll learn how to create a virtual item shop where players can spend their virtual currency to purchase in-game items. We'll set up a Lua script that lists available items, handles transactions by deducting the appropriate amount of virtual currency, and simulates giving the item to the playerโ€”all managed via the VolticHost Game Panel. ๐Ÿš€


๐Ÿ“Œ Prerequisites

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

  • Access to the VolticHost Game Panel

  • A previously implemented virtual currency system (refer to the "Implementing a Virtual Currency System" tutorial)

  • Basic knowledge of Lua scripting and server file management

  • A list of virtual items with their prices


1๏ธโƒฃ First Step - Creating the Virtual Item Shop 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 shop_system.lua.

  5. Open shop_system.lua for editing and paste the following code snippet:

    if SERVER then
        -- Define available shop items
        local shopItems = {
            ["health_pack"] = {price = 50, itemName = "Health Pack"},
            ["ammo_box"] = {price = 75, itemName = "Ammo Box"}
        }
        
        -- Command for players to purchase an item
        concommand.Add("buy_item", function(ply, cmd, args)
            local itemKey = args[1]
            if not itemKey or not shopItems[itemKey] then
                ply:ChatPrint("Invalid item. Usage: buy_item <item_key>")
                return
            end
            
            local price = shopItems[itemKey].price
            local steamID = ply:SteamID()
            -- Assume playerCurrency table is available from the virtual currency system
            if playerCurrency[steamID] and playerCurrency[steamID] >= price then
                playerCurrency[steamID] = playerCurrency[steamID] - price
                ply:ChatPrint("You purchased a " .. shopItems[itemKey].itemName .. " for $" .. price)
                -- Insert code here to give the item to the player
            else
                ply:ChatPrint("Insufficient funds!")
            end
        end)
    end
    
  6. Save the file after editing.

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

โœ… Virtual item shop script created and loaded successfully!


2๏ธโƒฃ Next Step - Testing and Enhancing the Shop

  1. Join your server and open the in-game console.

  2. Test the shop by entering a command such as:

    buy_item health_pack
    
  3. Check that the script correctly deducts the virtual currency and displays a confirmation message.

  4. If needed, edit shop_system.lua to add more items or adjust pricing.

  5. Restart your server after making any changes to test the updated shop.

โœ… Virtual item shop tested and enhanced successfully!


๐ŸŽ‰ Conclusion

You have now successfully created a virtual item shop on your Garry's Mod server using the VolticHost Game Panel! Players can now spend their virtual currency to purchase items, making in-game transactions more engaging. ๐Ÿš€

For more assistance, contact VolticHost Support.