The most effective solution to work with players' data

Hello everyone!

I’m currently developing a dungeon game, and I have a couple of questions regarding player data storage. I am currently using a module script + datastore. However while developing more complex spell-related mechanics I realized I need to update user data frequently, particularly for the interface. There’s a solution i’m using right now:

function PlayerData:UpdateClient(player)
	local data = self:Get(player)
	if not data then
		return
	end
	local character = player.Character
	if not character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then
		return
	end
	UpdateStats:FireClient(player,{
		Health = humanoid.Health,
		MaxHealth = humanoid.MaxHealth,
		Mana = data.Stat.CurrentMana,
		MaxMana = data.Stat.MaxMana,
	})
end

task.spawn(function()
	while true do
		task.wait(0.1)
		for _, player in ipairs(players:GetPlayers()) do
			PlayerData:UpdateClient(player)
		end
	end
end)

I do realize that a 0.1 second delay is too short, especially for large servers. However I recently learned about metatables. So i thought it would be better to work with them but before rewriting my data system i decided to ask some expierenced developers(you) about it.

I would be grateful to hear any suggestions on this matter. :smiley:

1 Like

I can also show the full script if you need it

1 Like

Why even update every 0.1 second? Isn’t health and maxHealth supposed to be constant on spawn?

I also plan to implement a character stat upgrade system, so I need to store at least the maximum health value in the module.

You can update interface when the value changes.
Humanoid:GetPropertyChangedSignal("Health")

Updating every second for no reason is highly inefficient.

That is exactly why I am here.

Let’s assume this solves the health issue.

However, the mana problem remains. Is there a similar method for tables?

And can you say anything about this?

Could you outline a bit more about what mana exactly is?

Additionally you can explore about meta tables here.

It is a resource spent on casting spells.

If that’s a numerical value, yes, the same listener mentioned here works.

So, does this method also work with tables inside a module script?

As you can see, I store mana in a table rather than in attributes.

Or should I also store the mana in the player’s attributes?

You can simply update the client when the value of MaxMana/CurrentMana is changed, if it’s an instance, use that property. If it’s not, the same place where you update the Mana, you can trigger the update to all clients along with changing the table.

1 Like

I am certainly aware of that; however, I am already modifying mana values ​​across many scripts (I have a pair of client-server scripts for each spell), and I am interested in being able to detect changes in player data from any script in the game. Of course, if no other solution proves as effective as I need, I will revert to the solution you suggested.

1 Like

Typically, a client value isn’t updated multiple times in server scripts at once. Have a bindable event to all other scripts called from one script, if you still wish to use many scripts on server side.

I’m going to try adding the changes to each script individually, because after reading your thoughts, I realized that what I wanted to do would be very difficult to implement. Thanks for the advice, though. :grinning_face:

1 Like

You can store values in player, for example, Coins here is a NumberValue:


Create them when player joins the game.
Listen for changes on client side.
These values can be changed on server-side only.

attaching file as an example:

  • player has the Coins NumberValue
  • server runs a thread that increments coins each 3 seconds
  • there’s a sample UI:
    image
    Coins listens for changes of the value and updates ui
  • as a test added TextBox and TextButton to change the value, which works on client side only, that is, does not replicate to the server

number_value.rbxl (73.6 KB)

similarly, when can add other values to players you want to listen.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.