How can i fix my datastore script?

So basically i am having trouble trying to understand how datastorage works, i’ve followed a tutorial on youtube to create my own script but it didn’t work even though there where no erros in the output and i even received the “Data Saved” on the output.

Resume of my problem: I leave the game, get the “data saved” print on output, but when i rejoin the game all my stats have been reseted

(I already published the game, tested it and it still didn’t save data)

Script:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player) -- WHEN PLAYER JOINS --
	
	local Folder = Instance.new("Folder") -- CURRENT STATS --
	Folder.Name = "Stats"
	Folder.Parent = player
	
	local Gold = Instance.new("IntValue")
	Gold.Name = "Gold"
	Gold.Value = 0
	Gold.Parent = Folder
	
	local Silver = Instance.new("IntValue")
	Silver.Name = "Silver"
	Silver.Value = 0
	Silver.Parent = Folder
	
	local Bronze = Instance.new("IntValue")
	Bronze.Name = "Bronze"
	Bronze.Value = 0
	Bronze.Parent = Folder
	
	local Rushies = Instance.new("IntValue")
	Rushies.Name = "Rushies"
	Rushies.Value = 0
	Rushies.Parent = Folder
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = Folder
	
	local Exp = Instance.new("IntValue")
	Exp.Name = "Exp"
	Exp.Value = 0
	Exp.Parent = Folder
	
	local MaxExp = Instance.new("IntValue")
	MaxExp.Name = "MaxExp"
	MaxExp.Value = Level.Value*100
	MaxExp.Parent = Folder
	
	local Leaderboard = Instance.new("Folder") -- LEADERSTATS STUFF --
	Leaderboard.Name = "leaderstats"
	Leaderboard.Parent = player
	
	local LeaderboardWins = Instance.new("IntValue")
	LeaderboardWins.Name = "Wins"
	LeaderboardWins.Value = Gold.Value
	LeaderboardWins.Parent = Leaderboard
	
	local LeaderboardLv = Instance.new("IntValue")
	LeaderboardLv.Name = "Lv"
	LeaderboardLv.Value = Level.Value
	LeaderboardLv.Parent = Leaderboard
	
	local function UpdateLeaderstats()
		LeaderboardWins.Value = Gold.Value
		LeaderboardLv.Value = Level.Value
	end
	
    Level.Changed:Connect(UpdateLeaderstats)
    Gold.Changed:Connect(UpdateLeaderstats)
	
	Exp.Changed:Connect(function()
		if Exp.Value >= MaxExp.Value then
			Level.Value = Level.Value + 1
			Exp.Value = 0
			MaxExp.Value = MaxExp.Value+50
		end
		
    local success, errormessage = pcall(function()
	local data = myDataStore:UpdateAsync(player.UserId)	
    if data then
        data.goldTrophy = player.Stats.Gold.Value
        data.silverTrophy = player.Stats.Silver.Value
        data.bronzeTrophy = player.Stats.Bronze.Value
        data.rushies = player.Stats.Rushies.Value
        data.level = player.Stats.Level.Value
        data.exp = player.Stats.Exp.Value
        data.maxExp = player.Stats.MaxExp.Value
		else
		print("There was an error while saving data!")
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player) -- WHEN PLAYER LEAVES --
	local success, errormessage = pcall (function()
	 local data = {}
		data.goldTrophy = player.Stats.Gold.Value
        data.silverTrophy = player.Stats.Silver.Value
        data.bronzeTrophy = player.Stats.Bronze.Value
        data.rushies = player.Stats.Rushies.Value
        data.level = player.Stats.Level.Value
        data.exp = player.Stats.Exp.Value
        myDataStore:SetAsync(player.UserId, data)
	end)
	if success then
		print("Data Saved!")
	else
		print("There was an error while saving data!")
		warn(errormessage)
	end
end)
end)

I really dont get what i did wrong

Did you just publish the game or did you go to published and played it from roblox website like any other game? and not from studio.

Published and played it, but yet it doesn’t work

How would you see the output after you left the game?

I test it first on Studio, so when i stop playing in there it shows the following in the output:

image

Then i play the published game to test but it doesn’t do anything

Try this:

local Data = game:GetService("DataStoreService"):GetDataStore("Data1") -- Change To Data2 to reset all stats

game.Players.PlayerAdded:Connect(function(player) -- WHEN PLAYER JOINS --
	
	local Folder = Instance.new("Folder") -- CURRENT STATS --
	Folder.Name = "Stats"
	Folder.Parent = player
	
	local Gold = Instance.new("IntValue")
	Gold.Name = "Gold"
	Gold.Value = 0
	Gold.Parent = Folder
	
	local Silver = Instance.new("IntValue")
	Silver.Name = "Silver"
	Silver.Value = 0
	Silver.Parent = Folder
	
	local Bronze = Instance.new("IntValue")
	Bronze.Name = "Bronze"
	Bronze.Value = 0
	Bronze.Parent = Folder
	
	local Rushies = Instance.new("IntValue")
	Rushies.Name = "Rushies"
	Rushies.Value = 0
	Rushies.Parent = Folder
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = Folder
	
	local Exp = Instance.new("IntValue")
	Exp.Name = "Exp"
	Exp.Value = 0
	Exp.Parent = Folder
	
	local MaxExp = Instance.new("IntValue")
	MaxExp.Name = "MaxExp"
	MaxExp.Value = Level.Value*100
	MaxExp.Parent = Folder
	
	local Leaderboard = Instance.new("Folder") -- LEADERSTATS STUFF --
	Leaderboard.Name = "leaderstats"
	Leaderboard.Parent = player
	
	local LeaderboardWins = Instance.new("IntValue")
	LeaderboardWins.Name = "Wins"
	LeaderboardWins.Value = Gold.Value
	LeaderboardWins.Parent = Leaderboard
	
	local LeaderboardLv = Instance.new("IntValue")
	LeaderboardLv.Name = "Lv"
	LeaderboardLv.Value = Level.Value
	LeaderboardLv.Parent = Leaderboard
	
	local function UpdateLeaderstats()
		LeaderboardWins.Value = Gold.Value
		LeaderboardLv.Value = Level.Value
	end
	
    Level.Changed:Connect(UpdateLeaderstats)
    Gold.Changed:Connect(UpdateLeaderstats)
	
	Exp.Changed:Connect(function()
		if Exp.Value >= MaxExp.Value then
			Level.Value = Level.Value + 1
			Exp.Value = 0
			MaxExp.Value = MaxExp.Value+50
		end
	end)
	
	local uniqueKey = "id-"..player.UserId
	local getSaved = Data:GetAsync(uniqueKey)
	if getSaved then
		Gold.Value = getSaved[1]
		Silver.Value = getSaved[2]
		Bronze.Value = getSaved[3]
		Rushies.Value = getSaved[4]
		Level.Value = getSaved[5]
		Exp.Value = getSaved[6]
		MaxExp = getSaved[7]
	else
		local valuesToSave = {
			Gold.Value,
			Silver.Value,
			Bronze.Value,
			Rushies.Value,
			Level.Value,
			Exp.Value,
			MaxExp.Value,
		}
		Data:SetAsync(uniqueKey, valuesToSave)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local uniqueKey = "id-"..player.UserId
	local valuesToSave = {
			player.leaderstats.Gold.Value,
			player.leaderstats.Silver.Value,
			player.leaderstats.Bronze.Value,
			player.leaderstats.Rushies.Value,
			player.leaderstats.Level.Value,
			player.leaderstats.Exp.Value,
			player.leaderstats.MaxExp.Value,
	}
	Data:SetAsync(uniqueKey, valuesToSave)
end)
1 Like

It worked, thank you, but would you mind explaining what you did? i am new to datastorage and would like to understand it better

Not the guy you’re replying to, but a highly suggest a module called DataStore2.

If you use the code you’ve written, you’re going to have data loss.

Because of some pretty major glaring flaws like using :SetAsync (Stop using SetAsync() to save player data), and updating data too fast (ie. updating after .Changed will make you hit the data rate limit extremely fast).

1 Like

Ironically i have just discovered DataStore2 before you said so xD, i am currently watching AlvinBlox video and trying to understand how it should work, hope i can make it work properly

1 Like

Just a question about what you’ve said related to “hitting the data rate limit”.

The only times i use the .Changed is to visually update the leaderstats based on the current Stats of the player and when the Exp reaches the MaxExp, i believe this has nothing to do with the datastorage since it only saves when the player leaves, or i am mistaken?

1 Like

Yeah you’re fine, I read it wrong, sorry. It’s still sound advice to use DataStore2 though.

1 Like

Oh ofc, i am looking forward to it

I used a datastore I scripted a while back for my game. It’s more simple and it’s not as complicated as yours and it’s easier to add more values to save if you add more stats in the future. The thing wrong with yours was that you were searching for the value inside the player when the values were already set above the code. You only needed to search for the values when you did the player leaving function. Also, you tried to update the async before anything was set.

1 Like

Hmm, i think i understand.

I have a question, is it possible to implement DataStore2 in the script you’ve sent me?

I haven’t used DataStore2 before and DataStore2 isn’t implemented by ROBLOX. It’s a module made from a developer. AlvinBlox has a tutorial explaining how to use it. Make a new script and follow the tutorial and then transfer your values over to the DataStore2 script.