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.
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.
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.
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.