So I recently came up with a game idea where you can buy, sell and trade items.
What I want to achieve is limited stock items. So basiclly theres a set value of stock (for instance 10) and every time a player buys the item the value drops by 1 on all servers (so only 10 items can be bought)
I think this would be done with MessagingService, but I have no idea how to properly use it and also I want the stock value to save if no servers are running.
Help would be very welcome.
EDIT: I actually got an idea:
Everytime a player buys an item, a message in MessagingService will be sent to all servers, containing the item name and the amount of stock left, both in a table. As for saving, maybe everytime a server closes save the stock to a datastore? Not sure on that one though
Would this work at all? Dont have time to check it out for myself right now
EDIT2: Closed down this topic since this is way more complicated than I thought it would be at first.
A way you could do this (from what I am thinking) is that you could do it so when a player buys the limited item, the server removes 1 from the limited stock value (Using a data store, but before you remove the money from the client you should check to see if the stock counter is 0 or below). To keep this value up to date you would need the server to know every time it changes so it could change the text for the amount of stock left.
I actually got an idea:
Everytime a player buys an item, a message in MessagingService will be sent to all servers, containing the item name and the amount of stock left, both in a table. As for saving, maybe everytime a server closes save the stock to a datastore? Not sure on that one though
Would this work at all? Dont have time to check it out for myself right now
Correct me if I’m in error, you can make a value in datastore, if you want a global limit of products then, you can check that value when changes and change the stock limit based on it.
EDIT: for datastore value changing check this: https://scriptinghelpers.org/questions/40454/how-to-check-if-datastore-value-changed This is the easiest solution that comes in my mind.
Yeah I was worried about using datastore for this, hence why in my edit I said to save the stock to datastore only when a server closes. As for HTTPService, never used it, never knew how it worked, never even looked into it. I guess this project is too much for my knowledge right now.
Can be solved easily, you must make a global debounce, with also using datastore. When the function of purchasing something is done, then the global debounce value will be active for an amount of seconds (1-6 seconds maybe).
HttpService, if I remember, you gotta make a website and that stuff, which is something harder to do than Lua scripting.
I’m thinking you could achieve this only with MessageService and DataStore.
Detect when the last player on the server leaves to save the stock data, and load the data when a new server is created.
Use the message service to update stock across servers. Should work fine, won’t be surprised if sometimes stock will be one above or one less than the other servers, though.
You may want to implement some sort of queue system to act as a sort of middle man between your player’s interactions and the check for availability.
For instance. If two players and a request to purchase an item, you will likely run into multiple transactions being invalid due to “stock” being acquired.
In essence you need to build a “Request to purchase” queue, which then a database server them looks at and processes the transactions.