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
-
Log in to VolticHost Game Panel.
-
Navigate to the File Manager from the main dashboard.
-
Go to the
/garrysmod/lua/autorun/server/
directory. -
Create a new file named
shop_system.lua
. -
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
-
Save the file after editing.
-
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
-
Join your server and open the in-game console.
-
Test the shop by entering a command such as:
buy_item health_pack
-
Check that the script correctly deducts the virtual currency and displays a confirmation message.
-
If needed, edit
shop_system.lua
to add more items or adjust pricing. -
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.