Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- When creating a table in lua, both commas and semicolons mean the same thing. As it happens, you could reorganize your table creation like this and it would mean the same thing:
- `local products = {50, 100, 250, 500, 1000, 1500, Gold50 = 0, Gold100 = 0, etc}`
- (Note that `0` is the same as `000000` in lua.)
- What you're wanting is to have a table within a table. You have a number of options available for organization:
- 1. Use `{Gold50 = {0, 50}, etc}`
- 2. Use `{Gold50 = {ID=0, Price=50}, etc}`
- 3. Use `{0 = {Name = "Gold", Offers = {{Amount = 50, Price = 50}, {Amount = 100, Price = 100}, otherDealsHere}}, 1 = {Name = "Name of item ID1", etc}}`
- 4. Use `{Gold = {[50]=50, [100]=100, etc}, etc}`
- Option 1 is the closest to what you're already trying to do, but it is the least descriptive -- when you're using it in your code you'll have cryptic `product[2]` phrases that will only make sense if you've looked at the `products` table relatively recently and remember that `product[2]` means `price`
- Option 2 is an improvement over #1 because it uses keys to describe what each value is. With this one, you do things like `product.Price`. If you prefer, you can get a similar effect by using option 1 with constants (which I like to put in ALL-CAPS to indicate that it is a constant):
- ~~~~~~~~~~~~~~~~~
- local products = {Gold50 = {0, 50}, etc}
- local ID = 1
- local PRICE = 2
- --elsewhere:
- local product = products.Gold50
- local price = product[PRICE]
- ~~~~~~~~~~~~~~~~~
- Option 3's key is the ID of the product; its value includes a listing of all the offers and their associated prices. I don't recommend using it, but I leave it there as an illustration of what you can do with nested tables.
- Option 4 skips the ID and has a simpler format than option 3 -- I highly recommend considering this unless you really need the ID for something. Why use a number when you could use a string? Well, encoding it in the datastores would be one reason, and ensuring that you can change the name later would be a 2nd, but if you do need the ID, you can just do this: `{Gold = {ID=0, [50]=50, [100]=100, etc}, etc}` The "[50]" is the key, the "50" is the value - I suggest that the key is the amount of gold you're offering and the value is the price you're selling it for. Notably, that "50" is not a string (though you could make it so by putting it in quotes).
- Another option to consider is to use classes. This is really only needed if you want to attach functions to your tables. A simple way of setting them up in this case (using option 4):
- ~~~~~~~~~~~~~~~~~
- function Product(id, name, offers)
- local self = {}
- -- Option 1: assign variables directly to self
- self.id = id
- self.name = name
- self.offers = offers
- -- Option 2: keep the variables private (so don't have the above 3 lines) and have functions for acessing them
- function self:GetID() return id end
- --same idea for the other 2
- --Now you put any other functions here, like:
- function self:GetPriceForOffer(amount)
- return offers[amount] -- or "self.offers[amount]" if using option 1
- end
- return self
- end
- --You can use inheritance to to make this more useful:
- function Gold()
- local self = Product(0, "Gold", {[50]=50, etc})
- function self:ApplyOffer(player, amount)
- _G.awardGold(player, amount)
- end
- return self
- end
- ~~~~~~~~~~~~~~~~~
- Even if you don't want to use classes, I recommend at least using a function:
- ~~~~~~~~~~~~~~~~~
- local products = {}
- function CreateProduct(id, name, offers)
- products[id] = {Name = name, Offers = offers}
- end
- CreateProduct(0, "Gold", {[50]=50, [100]=95, etc})
- ~~~~~~~~~~~~~~~~~
- The value of this is that you can change how you store `products` by changing a single line rather than rewriting your entire `products` declaration (which may have 10s or 100s of lines!) You can even do this:
- ~~~~~~~~~~~~~~~~~
- local products = {}
- function CreateProduct(id, name, offers)
- local product = {ID = id, Name = name, Offers = offers}
- products[id] = product
- products[name] = product
- end
- CreateProduct(0, "Gold", {[50]=50, [100]=95, etc})
- ~~~~~~~~~~~~~~~~~
- You can now access Gold with `products[0]` and `products.Gold` - whatever is needed!
- As for the rest of your script, you have a few more problems:
- - You should only assign to `ProcessReceipt` once (see [the API on it](http://wiki.roblox.com/index.php?title=API:Class/MarketplaceService/ProcessReceipt)), not every time a purchaseEvent occurs. It must handle all possible purchases. Possibly, each product should also have a function like `ApplyOffer` (which I demonstrated with the classes, but you can just add as a fourth argument to `CreateProduct` just as well). Then your ProcessReceipt function just needs to `product:ApplyOffer(player, amount)` -- much simpler than a whole series of if/elseif's! Also note that your structure is incorrect. Though it is true that ProcessReceipt will be called multiple times, you're supposed to return `Enum.ProductPurchaseDecision.PurchaseGranted` when you succeed (and you *should* only do this after saving what they purchased to a datastore *successfully*). Check the example in the link I provided at the beginning of this point for an example.
- - I recommend against using _G - ModuleScripts are meant to serve this purpose (and you can guarantee that the function has been declared since you first have to `require` it)
- - Minor note: You shouldn't need to make sure that `player` exists (at least based on the RemoteEvents tutorial); if somehow the information that the player left arrived to the server before the information that they requested a purchase, Roblox probably doesn't fire the OnServerEvent at all. Alternatively, if you'd like to be guaranteed error-free, you might also make sure that `player.Parent` exists also: `if player and player.Parent then`. You can save on an indent for the rest of your function by inverting it and returning: `if not player or not player.Parent then return end` - however, this is a stylistic choice.
Advertisement
Add Comment
Please, Sign In to add comment