Limited stock shop with HTTPService

So, a while back I asked how I could make a limited stock shop.

The answer was HTTPService, so I’ve tried using a Trello API (basiclly I had a board on trello, which had a ITEMS category, which had cards in it, which were named like the items I wanted to sell, and the description of the card was the amount of stock left), but that didnt work quite well (the latency was pretty bad and if you spammed the buy button the stock would get a negative value)

So I’m asking this:
Do I need to make my own website for this to work?
How would I code the game to be able to communicate with the website?
If I need to make my own website, how would I do that?

EDIT: Also, what is the latency of HTTPService?

Help would be appreciated.

3 Likes

The idea of a HTTPService oriented shop is cool, but is that really needed?

You can simply store the amount of items inside of a special inside of the datastore, adn each time someone buys that item, update the value by undecrementing it by 1.

What about datastore limits?
These are gonna be quickly hit

How so?
This is much simpler than you’d expect. It’s just one int value being stored, and getting updated every once in a while. If you have problems perhaps use Datastore2

This page should include all of DS’s limits

Okay, so I was about to say something about that “just 1 int value being saved” (since there would be more than 1 item), but I realised I could save all of the stock in a table.

The bigger question is - how (or when) would I save and load the data?

The data would be saved whenever someone buys the item, and for the loading, you just have to load it once each time the item is bought, store it in a variable, and use that variable for two things:
First, check if there are more items in stock before approving the purchase, meaning the loaded int value is > than 0. Then update the save value, by saving it again to the previous value - 1.

Does this shop need to have the same stock on all servers or is each shop stock just for one server?

It needs to be the same for all servers

Something I’ll look into is Cross-Server Messaging | Documentation - Roblox Creator Hub

This allows for messaging across servers and may be better than using data store gets and sets everytime someone buys stock.

1 Like

I was considering MessagingService, however, how would I load all of the stock on new servers?

You could use a datastore to keep track of it. You would just have to save this every minute or 30 seconds instead of every purchase. As well, every few minutes you can check what the datastore has vs what you think you have on each server to fix for any count errors.

1 Like

Ah! That seems like a nice solution!
I’ll try that later, because I can’t right now.

Let us know how it goes. I personally thought about doing something like this except it would be like an auction house. At the time I had my own server and planned on doing this with HTTPService but that was before the messaging service.

1 Like

Already ran into an issue.
So I did use MessagingService, however, how do I know which items stock is being sent through MessagingService?

Because I cant do for instance: MessagingService:PublishAsync(“StockChange”, “Mug”, 3)

The only data recieved is “Mug” which is just the name of the item, the stock doesn’t get sent through.

How do I fix this?

You could just send “Mug:3” and then do Table =string.split(String) and then you could do Table[1] (Mug) and Table[2] (3)

Oh, I feel kinda stupid now.

Thanks for the help!

If you feel like you’ve got your answer make sure to mark the answer as a solution so others can quickly go to the solution and also know that your problem has been resolved.

I think I ran into another issue.
Everything works fine, but if you spam the buy button you can get up to 11 Mugs (Even though the max is 5)

Now, that seems like a simple fix, just add a debounce to the button, and yes I did do that, HOWEVER what if 2 people in different servers press the button at once when the stock is 1?

Pretty sure it will change to -1

You can add a simple check to check if after the person buys the mug it doesnt go over the max limit and if it does it tells them instead of buying.

Well, I already have that

if tool1.CurrentStock.Value > 0 then

But I think that MessagingService doesn’t update the CurrentStock value fast enough.