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

Why is my data store not saving? Im Trying to save leaderstats when the player leaves!

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("Yeetsavesystem1221")
local ds2 = datastore:GetDataStore("LifeSaveSystem31132aw")

game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local yeets = Instance.new("IntValue", folder)
 yeets.Name = "Yeets"
 local Life = Instance.new("IntValue", folder)
 Life.Name = "Life"
 
 yeets.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, yeets.Value)
 
 Life.Value = ds2:GetAsync(plr.UserId) or 0
 ds2:SetAsync(plr.UserId, Life.Value)
 
 yeets.Changed:connect(function()
  ds1:SetAsync(plr.UserId, yeets.Value)
 end)
 
 Life.Changed:connect(function()
  ds2:SetAsync(plr.UserId, Life.Value)
 end)
end)

2 comments

1

are u trying to save when they join? Thats what im looking at

0

if I helped you out please accept my answer. Otherwise comment how I can help you better.

2 answers

Ok so there are two main problems in your script that I will try to address. First: Never use a Changed event in conjunction with Set/Update Async. It will cause the server to throttle your requests. There is a limit to how many datastore requests you can post in a certain amount of time. I don't know the exact amount off the top of my head, but if you did some research I bet you could figure it out. The second problem is not as bad but can cause some issues. You really should be using pcall to protect your script from breaking when a request goes wrong. Now that I have explained the problem I will propose some solutions. First: You can save the players data in a loop and when they leave. The loop is just in case something goes wrong and the game crashes. Here is an example:

while wait(180) do -- you can adjust the wait time to your liking

	for i,player in pairs(game.Players:GetChildren()) do

		-- save players data

	end
end

Now to address the second issue. Here is how pcall can be used to protect your script:

local success, message = pcall(function() -- because pcall returns two values we will allow for both 
	-- here we save datastore:SetAsync() or whatever

end)
if success then
	print("player data saved wonderfully")
else
	print("player data failed to save error-"..message)
end

the first variable success(you can call it what you like) is always a boolvalue when used with pcall. if the code inside the pcall fails it will equal false. the second variable will always equal nil unless success = false then it will be whatever error message was generated. I used the if statement to tell me if the players data saved and if not to tell me the error. You don't need to do that part of pcall but I have found it really useful. Hope this helps and have a great day scripting!

Note: The code after the pcall will still run even if the code inside fails.

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