Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why don't the stats always save on my game?

This has been happening for a while but, it seems that the script I made doesn't always save peoples stats in my game, and I'm worried as I am starting to be threatened with people saying they will make Alts and spam dislike my game if I don't do anything about it. This is the code for one of the stats (There are 4 of them, which are all identical besides that they are "KiData", "AgilityData" and "MasteryData"):

local module = {}
local Storage = game:GetService("DataStoreService"):GetDataStore("StrengthData")


game.Players.PlayerAdded:connect(function(plr)
	local StrengthData = Instance.new("StringValue", plr)
	StrengthData.Name = "StrengthData"
	
	local Strength = Instance.new("NumberValue", StrengthData)
	Strength.Name = "Strength"
	Strength.Value = math.random(1,300)
	
	local SavedStats = Storage:GetAsync(plr.userId .. "-Strength")
	if SavedStats ~= nil then
		Strength.Value = SavedStats
	end
	
	while true do
	wait(0.1)
	if StrengthData.Strength.Value > 1000000000000000000 then
		StrengthData.Strength.Value = 1000000000000000000
	end
end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local id = plr.userId
	local StrengthValue = plr.StrengthData.Strength.Value
	Storage:SetAsync(id.. "-Strength", StrengthValue)
end)


return module

The script resides in the ServerScriptService, and is a Module Script inside a normal script in the ServerScriptService which only has one line of code:

require(script:WaitForChild("ModuleScript"))

So could somebody please tell me what isn't working here? If there is a fix to stop people losing stats from my game, that would be very helpful if you manage to find it, thank you.

1 answer

✓ Accepted answer

Problems:

  • This is a ModuleScript. If you don't intend to have any other scripts using this one (ex by asking what a player's current Strength is -- you could do this instead of using NumberValues), you might as well make it a Script.
  • You have 4 nearly identical scripts? That's breaking a very important programming guideline: Don't Repeat Yourself (DRY). You could put all the stats in a list and use for loops to interact with them. The reason for this is because any modifications you make to one script you have to make to all of them (which is quite a hassle, especially if you have a bunch of changes).
  • That while true do loop errors when the player leaves. Consider instead while plr.Parent do and put the wait after the if block. Also consider only decreasing the value on save (and maybe when you increase it), rather than 10x/sec
  • You use a StringValue for "StrengthData", but what's the point of this? You could take it out and parent Strength to plr instead.
  • Instance.new(type, parent) is deprecated -- you're not supposed to provide the parent (due to efficiency reasons). Assign the parent after changing the name/value.
  • (Style) Lines 19-23 should be indented once more
  • (I think this is the problem you're having) Get/SetAsync can randomly error. You must call them in a pcall so that if they error, the rest of your function can continue to execute. Further, if pcall returns false, that means it did error and you should try it again in a few seconds -- give it a few tries before giving up (and if GetAsync fails completely, you should let the player know that their data didn't load; you probably shouldn't try to overwrite their saved data when they leave, either, unless the player agrees to reset their save data).
  • Some datastore scripts also do periodic saves (ex once a minute) so that if the datastore is down when the player leaves, they won't lose much progress.
  • Ideally you would load/save all the stats in a table or dictionary, or else it will cost you 4 requests every time a player enters/leaves (more if any of those requests error) -- a player only gives you 10 a minute!

This discussion is preserved as it was. New answers, comments, and votes are closed.