Script doesn't save values correctly [DataStore]

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I’m trying to make a level system that works with 3 values, which should save.

  2. What is the issue? The data doesn’t save correctly, it always changes all of the values to 99.

This is the script:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local LevelFolder = Instance.new("Folder")
	LevelFolder.Name = "LevelFolder"
	LevelFolder.Parent = player
	
	local ReqExp = Instance.new("NumberValue")
	ReqExp.Name = "ReqExp"
	ReqExp.Parent = LevelFolder
	ReqExp.Value = 10
	
	local PlayerLevel = Instance.new("NumberValue")
	PlayerLevel.Name = "PlayerLevel"
	PlayerLevel.Parent = LevelFolder
	PlayerLevel.Value = 0

	local PlayerExp = Instance.new("NumberValue")
	PlayerExp.Name = "PlayerExp"
	PlayerExp.Parent = LevelFolder
    PlayerExp.Value = 0
	
	local data
	local succes, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-reqexp")
		data = myDataStore:GetAsync(player.UserId.."-playerlevel")
		data = myDataStore:GetAsync(player.UserId.."-playerexp")
	end)
	if succes then
		ReqExp.Value = data
		PlayerLevel.Value = data
		PlayerExp.Value = data
	else
		warn(errormessage)
		print("There was an error whilist getting your data")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local succes, errormessage = pcall(function()
	myDataStore:SetAsync(player.UserId.."-reqexp",player.LevelFolder.ReqExp.Value)
	myDataStore:SetAsync(player.UserId.."-playerlevel",player.LevelFolder.PlayerLevel.Value)
	myDataStore:SetAsync(player.UserId.."-playerexp",player.LevelFolder.PlayerExp.Value)
	end)
	
	if succes then
		print("Player Data succesfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
	
end)
  1. What solutions have you tried so far? I’ve checked the Developer Hub, but I haven’t found anything that could be the issue. I thought it could’ve been a limit of Data Stores but I think it isn’t.

Why are you saving the players Required XP?
Instead of saving the player’s Required XP, use an equation to get the amount of XP the player needs for each level.

To the actual question, you have a variable named data, and its getting overwritten every time you do data =. Use separate variables to fix this.

Several red flags in your code.

  • Setting values after parenting your ValueObjects. Only parent once you’ve set all properties.

  • Using three separate keys for three values instead of just putting this into a table and using one key per player per DataStore (both should be only 1). You now use 3 requests per player when they enter and three when they leave. I smell throttling.

  • Using pcall wrongly. One pcall per operation. Then again, if you just consolidate to one DataStore with a table, you would only ever need one pcall in the first place. You also aren’t making use of the success values or the fact that pcall is an error handler so if a DataStore fails, you’re going to get data loss reports.

  • In addition to the above, you’re overwriting the data variable three times. Data will become the last value which it was set to, which is the result of the fetch from UserId-playerexp. This data gets used as the value for all three: RequiredExp, PlayerLevel and PlayerExp.

  • Unnecessary storage of data. Instead of using an arbitrary equation to determine how much EXP is required for the next level based on their current one, you’re saving that as part of their data. Don’t do that.

I think fixing these issues doesn’t require a whole lot of effort so I won’t spend my time lumbering on about fixes since you can search, attempt and test for yourself, but if you need support I am more than happy to provide it. These points should serve as guiding words however.

4 Likes

Your problem is that you repeatedly change the data value that saves everything. To fix this make new definitions for it like this:

local data, data2, data3 -- you should use good names for your data value though, so you can remember which data is which

data = myDataStore:GetAsync(player.UserId.."-reqexp")
data2 = myDataStore:GetAsync(player.UserId.."-playerlevel")
data3 = myDataStore:GetAsync(player.UserId.."-playerexp")
-- continuing your script from there

edit: colbert2677 also gives a whole line on stuff to fix, but this is an example for you.

Thank you very much, I will try to use tables once I’ve figured out how to use them correctly. :slight_smile: