Custom Store Items - Foohy/jazztronauts GitHub Wiki

Adding custom items to the store currently requires a lua script, but it is pretty straight forward:

NOTE: These functions must be called on startup on both the client AND server.
Just put them in a shared file and it'll all be good.

Adding to the store

jstore.Register("weapon_name", price, {  
    name = "Pretty Store Name",  -- Name to be showed in store
    desc = "description",        -- Description of the item to be displayed in store
    type = "tool" or "upgrade",  -- Sets which store it appears in
    cat = "Weapon Category",     -- Optionally sorting category to put it in (Only applies to upgrades)
    requires = "itemname",       -- The name (or table of names) of store items that this requires be unlocked before purchasing
    thirdparty = true,           -- Sort non-jazz weapons to bottom to prevent confusion
})

Note that the first argument can also be a SWEP table:

jstore.Register(SWEP, 10000, { type = "tool" })

It will automatically use the weapon class name and description defined in the swep table.
Store items that match a weapon name will unlock that weapon.

Upgrades, and non-weapon items

Anything can be added to the store with the above method, it doesn't need to be a weapon.
This is how upgrades are handled.

Upgrades

To register a series of related upgrades (for example, Stan speed level 1, level 2, ....) use RegsiterSeries:

jstore.RegisterSeries("name", price, numberOfLevels, { 
	...
        -- Same options as normal item
        ...
	priceMultiplier = 2, -- Configure how much the price increases for each level.
})

Price is calculated like so:
local price = basePrice * math.pow(priceMultiplier, upgradeLevel - 1)

Querying for purchased items and upgrades

If you just want your weapon to unlock after being purchased in the store, you are done. You don't need to follow anything here.
However, if you want to add some weapon upgrades, or do any custom checking for purchased items, keep going.

When you use jstore.Register, it will give you an item name (usually the one you passed in) to check against.

To check if a particular user has a specific upgrade:

unlocks.IsUnlocked("store", player, "item_name")

This is shared and automatically networked. However, the local player can only query their own upgrades.

For repeating series upgrades, you can check each individual level, but there is a utility function to quickly get the maximum level of an upgrade for a user:

jstore.GetSeries(player, "upgrade_name")

This returns an integer indicating their current upgrade level for that item.
0 means they do not own it 1 means they've purchased the first upgrade etc.

Examples

You have a custom SWEP you want to add to the store, with an optional one-time upgrade to enable additional functionality.

Assuming this is in a single file, shared.lua, that will be run on both the serer and client.

- Add the weapon itself to the tools store
local storeName = jstore.Register(SWEP, 35000, { type = "tool" })

-- Additional turbo functionality that you implement later
local storeUpgradeName = jstore.Register("crazy_upgrade", 15000, { 
    name = "Crazy Upgrade", 
    cat = "TurboTool", 
    desc = "Enables extended functionality upgrade of TurboTool",
    type = "upgrade",
    requires = storeName -- To purchase this ugprade, they must have purchased the tool first!
})

The first entry adds the tool to the store, making it so when you purchase it, they will be able to use it.
The second entry adds an optional upgrade that makes the weapon perform different.

Now, the weapon must check for upgrades when it is created:

self.HasCrazyUpgrade = unlocks.IsUnlocked("store", self.Owner, storeUpgradeName) 

if self.HasCrazyUpgrade then
 -- Whatever you want, wherever you want!

Remember that the unlocks are already networked, there is no need to add any additional networking, datatable entries, or whatever to network whether the weapon is upgraded. It's already done for you.