Anyone know any improvements for this data saving script?

Hi! Like @FeudalLucy already said, you script is indeed pretty neat, so there are only a couple of minor changes to be done.

Firstly, this doesn’t affect your code at all, but you should name your module, for example, rename it to “DataManager” or something descriptive. Everything great, but there are some little changes for better to be done in Auto_Save coroutine.

  1. Use UpdateAsync() instead of SetAsync(). It helps you carefully update the previous data.

(image from Roblox Dev Hub | Data Stores)

  1. If I were you, I’d replace
while wait(time here) do

if favour of

while (true) do
-- wait here
end

To my knowledge wait() isn’t guaranteed to run reliably, so second option should be a way to go.

  1. Lastly, use game:GetService(“Players”) to find services. I also noticed the following line:
for UserId,_ in pairs(game.Players:GetPlayers()) do
	local Player = game.Players:GetPlayerByUserId(UserId) -- not needed
	-- further code
end

which is not really needed, because

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
-->> player value provides you the player object already
end

Lastly, you should probably add Players to the services list you have defined at the top.

Otherwise, again, you script looks quite neat, so I don’t currently see any other changes you should be doing.

Good luck with your project!

EDIT (2021-03-22)

UpdateAsync() works pretty well and is very welcome, but you can of course use SetAsync() for certain data types. It ultimately depends on what data you are saving. I found this useful reply by @posatta.

Take look at the important extension of this post!

2 Likes