Save Data Problem

I want to create an exotic, thrilling, extra-ordinary and amazing script that saves the data of the in-game player. However, I fell down to the knees when I understood when you join the game the value goes to 0 and not to the actual data.

local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("StrengthPulSimIDataStore")


game.Players.PlayerAdded:Connect(function(plr)
	task.wait(2)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	local Strength = Instance.new("IntValue")
	Strength.Parent = leaderstats
	Strength.Name = "Strength"
	Strength.Value = 1
	
	local success, errormsg = pcall(function()
		local data = MyDataStore:GetAsync(plr.UserId)
		Strength.Value = data
	end)
	if success then
		print("Ok")
		elseif errormsg then
		print("NotOk")
		warn(errormsg)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormsg = pcall(function()
	MyDataStore:SetAsync(plr.UserId, plr.leaderstats.Strength.Value)
	end)
	if success then
		print("success")
		elseif errormsg then
		print("error while saving the data")
	end
end)

Save yourself some headache and use ProfileService:
Save your player data with ProfileService! (DataStore Module) - Resources / Community Resources - Developer Forum | Roblox

1 Like

Have you tried troubleshooting by printing or manually checking plr.leaderstats.Strength.Value? To be sure that it’s actually nonzero. I assume that it’s being changed by a different script during gameplay, which could be messing it up somehow.

1 Like

Now, I made it like this, but seems like it didn’t get the Async

local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("StrengthPulSimIDataStore")


game.Players.PlayerAdded:Connect(function(plr)
	task.wait(2)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	local Strength = Instance.new("IntValue")
	Strength.Parent = leaderstats
	Strength.Name = "Strength"
	Strength.Value = 1
	
	local success, errormsg = pcall(function()
		local data = MyDataStore:GetAsync(plr.UserId)
		if data then
		Strength.Value = data
		print("Test")
			print(data)
		end
	end)
	if success then
		print("Ok")
		elseif errormsg then
		print("NotOk")
		warn(errormsg)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormsg = pcall(function()
	MyDataStore:SetAsync(plr.UserId, plr.leaderstats.Strength.Value)
	
	end)
	if success then
		print("success")
		elseif errormsg then
		print("error while saving the data")
	end
end)
1 Like

Add this to the end of the script:

game:BindToClose(function()
    task.wait(3)
end)

This will give the game time to save the data. If that doesn’t fix it, try testing the script outside of studio.

1 Like

Seconding this, I would also suggest reading my reply here because it applies nearly identically to this thread.

The code in this thread looks identical to another one.@atsitinis are you getting this code off of a youtube video or some resource? If so, can you link it because it’s teaching some extremely bad practices.

1 Like

Note that ideally you should just save the data of all the players in the servers for BindToClose.


@OP

There are a lot of problems with this code like improper use of pcalls.

One problem that could be causing this issue is:

GetAsync returns nil if there is no data, so if the player has no data the default value of 1 is over-written and replaced with zero (nil converted to a number).

You also should save all the players’ data with BindToClose.

I would highly recommend DataStore2. DataStore2 does all the complex checks and guarantees your code should do for you and is extremely proven. There are also many great tutorials on it and it’s pretty easy to use.

You can also use this no-code solution that saves leaderstats automatically:

I personally use this for small projects just because it’s so fast and simple (it also uses DataStore2 behind the scenes so it’s theoretically reliable). You just need to do :WaitForChild(“leaderstats”) when you want to get the leaderstats and you can put IntValues, Folders, and stuff in one of the script’s folders to automatically make default data.

Here are some simple instructions:

  • Add the script
  • Add your default data the defaultdata folder in the explorer (e.g. an IntValue named Strength)
  • When you want to get leaderstats do player:WaitForChild("leaderstats") (waits for it to load if needed)
  • Then you can access the strength IntValue and everything saves automatically.
1 Like

Hi, Raxel. Your script is almost there, but it needs a minor adjustment to handle the case where the player’s data has not been previously saved, which could be causing the value to reset to 0 instead of the actual saved data. When you retrieve data using GetAsync, if there’s no data saved for a given key (in this case, plr.UserId), GetAsync returns nil. You should check if data is nil and, if so, set Strength.Value to a default value (e.g., 1) instead of directly assigning data to Strength.Value, which could be nil.

Here’s the corrected segment of your script:

game.Players.PlayerAdded:Connect(function(plr)
    task.wait(2)
    local leaderstats = Instance.new("Folder")
    leaderstats.Parent = plr
    leaderstats.Name = "leaderstats"
    
    local Strength = Instance.new("IntValue")
    Strength.Parent = leaderstats
    Strength.Name = "Strength"
    -- Set a default value for Strength
    Strength.Value = 1
    
    local success, errormsg = pcall(function()
        local data = MyDataStore:GetAsync(plr.UserId)
        if data then
            Strength.Value = data
        else
            -- If no data, the default value set above remains.
            print("No data found for player, using default Strength value.")
        end
    end)
    if success then
        print("Data loaded successfully")
    else
        print("Failed to load data: " .. errormsg)
        warn(errormsg)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success, errormsg = pcall(function()
        MyDataStore:SetAsync(plr.UserId, plr.leaderstats.Strength.Value)
    end)
    if success then
        print("Data saved successfully")
    else
        print("Error while saving data: " .. errormsg)
    end
end)

Take care!

2 Likes

No, I scripted it, so that’s the reason why it is not good since I am far away from mastering data stores yet.

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