How should damage and health be handled properly in a game?

I’m looking for the most efficient and proper way to consider damage taken and health changes in my game.
Currently I’m using the DataStore2 module and have a constant feed of the most updated data being sent to the client, in order to display their Stats on a GUI.

The only option that comes to my mind, is to set the data store with the most current values of the health or mana, each time its changed.

The only thing I was considering before doing this, Is what happens when a single player is being attacked by multiple enemies at once. The damage event will be called to in a quickly manner, within a short period of time. Would this overload the users data (datastore2) ?
Below is an example of how I would increase a players level when they level up.

userData[slot].CharData.Level = userData[slot].CharData.Level + 1 --Increases the level
DataStore2(userDataStoreName, player):Set(userData)	--Sets & saves into datastore

An example I just came up with on how I think the health would work…

local function takeDamage(player, slot, damage)
    userData[slot].CharData.currentHealth = userData[slot].CharData.currentHealth - damage
    DataStore2(userDataStoreName, player):Set(userData)
end

Any ideas would help! Thanks.

You should instead save their data periodically (every minute or so) OR whenever the player dies or leaves the game. There’s really no need to have to update the datastore every single time the player takes damage.

1 Like

IIRC DataStore2 caches data so I don’t think it would send a new save request so I think you’ll be fine.

1 Like

Can confirm:

DataStore2 is an open source module that uses berezaa’s method of saving data that prevents data loss, caches, and verifies data before saving."

Which means that you can call :Set as often as you want, it only affects the cached version of the data. That’s one of the big selling points of using DataStore2 in the first place.

2 Likes

That’s what I figured, Thanks for the help!