Economy/Datastore duplication prevention

I am working on a project with an ingame economy but have run into a predicament. I had experienced a case where items in my game were duplicated, the method works as follows:

Say there are two players currently playing, and one of them gives 1x Iron Sword to the other. The second player logs out with his new sword, triggering a save. The first user then triggers a server crash (or the server crashes for some other reason), preventing an autosave or logout save. Now the first user has his sword back, and the second user has the traded sword as well, meaning an item was duped. I am not sure how to handle this. Currently the system works where it saves a user’s data after they logout, and periodically saves the data of all users every 5 minutes.

One option is to save after every transaction and item pickup, but this would throttle quickly.
Another is to save every player after a single player leaves, but I also see this throttling quickly.
If anyone has experience working with something similar, advice would be appreciated. Thanks.

Try saving data for a player each time the player leaves, after the transaction, just perform necessary alterations in the pure data you need to save later so as to avoid saving an older version.

When the player leaves only the data you kept in storage would be saved, so the latest version would be saved to the store.

Update the database on both users once the trade is completed.

1 Like

Try putting a unique ID on the item when it is obtained. If the item exists twice then give it back to the original owner and remove it from the other account.

1 Like

This is a solution that a lot of popular games use. It’s literally foolproof and can easily prevent duping.

How would this work though to prevent crash duping? If the servers crashed, those two players will no longer be in the same server together so checking if the item’s ID is unique wouldn’t be possible unless you checked against a complete database of every other player’s inventory. Even then it would still be vulnerable to duplicating things that clobber IDs like stackables or coins.

Currently my solution is to save at intervals (60 seconds). If a player leaves, their session is still considered active until the next save interval. This prevents people from being able to selectively trigger the save system to dup through crashing. The downside is if a player hops servers, they may have to wait for their old session to close (autosave) before being able to log in again. A partial fix I came up with though is using MessagingService to signal the player’s old server to autosave (if the requests per minute limit allows), reducing the wait time.

It’s not a perfect solution, but I haven’t been able to think of anything else. It seems this method of duplication has plagued various MMO games and is still not entirely solved. Here is a video describing how it works in Runescape still recently Item duplication is still happening in OSRS - YouTube

you can use messaging service to communicate across all your servers

What if you combine it with ProfileStore or a form of datastore session locking or even just only using session locking? I believe it solves the crashing problem entirely?

@DevZse MessagingService is not meant for critical information, it says on the documentation itself that it is not best if delivery is critical. MemoryStore would be the closest next best thing.