Data is not saving. Why?

  1. What do you want to achieve?
    I’m simply trying to create a data storage system for my game. I’ve tried many different solutions, however, none of them appear to be working.

  2. What is the issue?
    The issue is that I can’t find the problem with the code. I was hoping some of you could take a look at it and tell me what I’m doing wrong.

  3. What solutions have you tried so far?
    I’ve looked for solutions everywhere however none of them appear to be working. I’m convinced the problem has to do with my code (of course it is). I’ve already tried changing where the data was being stored (from Lighting to ReplicatedStorage), but still no luck.

In order to save the Data, I have created 2 scripts, one for creating the data, and one for saving it. Take a look, please.

– Here is the first script that creates the data (This is only a portion of the script, the parts I thought were necessary. If you need to see more of the script let me know.) –

game.Players.PlayerAdded:connect(function(plr)

--Create Leaderstats folder--

		local Leaderstats = Instance.new("Folder", game.ReplicatedStorage)
		Leaderstats.Name = plr.Name
		local Leaderstats = game.ReplicatedStorage:WaitForChild(plr.Name)

--Create values inside of folder--

		local Points = Instance.new("NumberValue", Leaderstats)
		Points.Name = ("Points")
		Points.Value = 0
		
		local Level = Instance.new("NumberValue", Leaderstats)
		Level.Name = ("Level")
		Level.Value = 1

		local UnlockKeys = Instance.new("IntValue", Leaderstats)
		UnlockKeys.Name = ("UnlockKeys")
		UnlockKeys.Value = 0

		local TotalMissionsCompleted = Instance.new("IntValue", Leaderstats)
		TotalMissionsCompleted.Name = ("TotalMissionsCompleted")
		TotalMissionsCompleted.Value = 0

end)

– Here is the second script that saves the data to DataStorage (This one is the WHOLE script.)–

-- Script will be used to save player's data --

local DataStorage = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(Player)
	
	wait(5)
	
	local PlayerId = ("Player-".. Player.UserId)
	
	local SaveLevel = game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("Level")
	local SavePoints = game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("Points")
	local SaveMissionsCompleted = game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("TotalMissionsCompleted")
	local SaveUnlockKeys = game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("UnlockKeys")
	
	local GetData = DataStorage:GetAsync(PlayerId)
	
	if GetData then
		
		SaveLevel.Value = GetData[1]
		SavePoints.Value = GetData[2]
		SaveMissionsCompleted.Value = GetData[3]
		SaveUnlockKeys.Value = GetData[4]
		
	else
		
		local Savings = {SaveLevel.Value, SavePoints.Value, SaveMissionsCompleted.Value, SaveUnlockKeys.Value}
		DataStorage:GetAsync(PlayerId, Savings)
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	
	DataStorage:SetAsync("Player-".. Player.UserId, {game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("Level").Value,game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("Points").Value,game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("TotalMissionsCompleted").Value,game.ReplicatedStorage:WaitForChild(Player.Name):WaitForChild("UnlockKeys").Value})

end)

Thank you so much to anyone who helps. :slight_smile:

  • Zipps

your trying to concate a number with a string and you cant save an array to a datastore, it must be string.

Thanks for the reply minimic2002,
How do you recon I change my code? I haven’t received any errors from the output so where should I start?

You need to see if the data is not nil like so:

if GetData ~= nil then
		
		SaveLevel.Value = GetData[1]
		SavePoints.Value = GetData[2]
		SaveMissionsCompleted.Value = GetData[3]
		SaveUnlockKeys.Value = GetData[4]
		
	else

Well id say look at the API for datastores so you can see them broken down.
Then look into string manipulation.
Better string manipulation means you can easily save and read your data.

To fix player
SetAsync(“Player-”… tostring(Player.UserId),Data)

Then for the data if you are trying to save multiple things combine them with table.concat(Table,Seperator). This mean that your table can be turned into 1 string that can be saved. Then to turn it back into string all you have to do is string.split()

https://developer.roblox.com/en-us/articles/Data-store
https://developer.roblox.com/en-us/api-reference/lua-docs/string
https://developer.roblox.com/en-us/api-reference/lua-docs/table

I’ll give it a look, thank you.

Are you sure this is the problem? I’m getting no errors in my output.

Try turning it all into 1 string and saving it that way, if that doesn’t work then ill be shocked.

It might also be worth using pcall() to catch errors as datastores can fail sometimes