Saving Multiple Values In Datastore Not Working

So I’m currently working on a level system for my game, but I’ve run into a problem while scripting the datastore for it. Instead of the amount of XP needed to level up is at 100, if the player hadn’t leveled up from level 1 and level value will be at 1, but the amount of XP needed to level up will only go as 0 instead of 100 and sometimes the other values will tend to turn to 0 including the player’s level. I would be able to level up still but I wanted the level to be 1 instead 0 once the player joins without any data. Here’s an Image:

Here’s the values in the player
Screenshot 2021-05-30 154955
Screenshot 2021-05-30 155058
The Value is supposed to be 100.

here’s the script:

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("LevelStatSave")
local data

local function createTable(player) --This will create a table to hold all the level stats
	local level_stats = {}
	for _, stat in pairs(player.LevelStats:GetChildren())do
		level_stats[stat.Name] = stat.Value
	end
	return level_stats
end

game.Players.PlayerAdded:Connect(function(player) --Once the player joined it will run the event
	local LevelStats = Instance.new("Folder", player) --Will create a folder the will hold all the stats
	LevelStats.Name = "LevelStats"

	local level = Instance.new("IntValue", LevelStats) --Will create the player's level
	level.Name = "Level"

	local exp = Instance.new("IntValue", LevelStats) --Will create the player's current XP
	exp.Name = "Current"

	local maxExp = Instance.new("IntValue", LevelStats) --Will create the player's Max XP they 
	maxExp.Name = "Max"

	--DataStore Load
	data = datastore:GetAsync(player.UserId)
	if data then
		level.Value = data["Level"]
		exp.Value = data["Current"]
		maxExp.Value = data["Current"]
	else
		level.Value = 1
		exp.Value = 0
		maxExp.Value = 100
	end
end)

--DataStore Save
game.Players.PlayerRemoving:Connect(function(player)
	local level_stats = createTable(player)
	local success, err = pcall(function()
		datastore:SetAsync(player.UserId, level_stats)
	end)

	if not success then 
		warn("Failed to save Level Stats!")
	end
end)

Does anyone know how to help me out with this, I’m still new to data stores so I’m not that experienced. Any help will be appreciated!

Did you mean to put the maxExp the same as the data["Current"]…? Also I’d just add a local data inside the PlayerAdded event, not sure why you’re putting it outside

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("LevelStatSave")

local function createTable(player) --This will create a table to hold all the level stats
	local level_stats = {}
	for _, stat in pairs(player.LevelStats:GetChildren())do
		level_stats[stat.Name] = stat.Value
	end
	return level_stats
end

game.Players.PlayerAdded:Connect(function(player) --Once the player joined it will run the event
	local LevelStats = Instance.new("Folder", player) --Will create a folder the will hold all the stats
	LevelStats.Name = "LevelStats"

	local level = Instance.new("IntValue", LevelStats) --Will create the player's level
	level.Name = "Level"

	local exp = Instance.new("IntValue", LevelStats) --Will create the player's current XP
	exp.Name = "Current"

	local maxExp = Instance.new("IntValue", LevelStats) --Will create the player's Max XP they 
	maxExp.Name = "Max"

    local data
	--DataStore Load
	data = datastore:GetAsync(player.UserId)
	if data then
		level.Value = data["Level"]
		exp.Value = data["Current"]
		maxExp.Value = data["Max"]
	else
		level.Value = 1
		exp.Value = 0
		maxExp.Value = 100
	end
end)

--DataStore Save
game.Players.PlayerRemoving:Connect(function(player)
	local level_stats = createTable(player)
	local success, err = pcall(function()
		datastore:SetAsync(player.UserId, level_stats)
	end)

	if not success then 
		warn("Failed to save Level Stats!")
	end
end)

Quick question: When you tested the datastore, did you change the value of max from the client or the server?

Not quite
, the maxExp is how much XP the player needs to level up and the current is the amount of XP the player has currently, and imma try putting the local data inside the function.

if you mean loading the data, I used the server. All know is that datastores can only be used in serverscriptservices.

What isn’t working loading or saving

Sometimes the level, the max Exp nevers saves or loads properly

image - 2021-05-30T172604.269
What they mean is did you click this and then change the value. And if you didn’t that means you’re still on client side and the server can’t save data from that.

no i didn’t change the value through client

Hm for me it is saving all values in studio

it’s probably the rest of the script messing with the values then, imma try to split the datastore off from the rest of it

Does the max value stay at 0?.

yes, and since the level system script is supposed to the times the max exp at a certain number depending the player’s level, 0 * something = 0. so it’ll always stay at 0 no matter what.

Just realized that you should probably encase your GetAsync function inside a pcall as well oof

Try this and see what outputs back:

local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("LevelStatSave")

local function createTable(player) --This will create a table to hold all the level stats
	local level_stats = {}
	for _, stat in pairs(player.LevelStats:GetChildren())do
		level_stats[stat.Name] = stat.Value
	end
	return level_stats
end

game.Players.PlayerAdded:Connect(function(player) --Once the player joined it will run the event
	local LevelStats = Instance.new("Folder", player) --Will create a folder the will hold all the stats
	LevelStats.Name = "LevelStats"

	local level = Instance.new("IntValue", LevelStats) --Will create the player's level
	level.Name = "Level"

	local exp = Instance.new("IntValue", LevelStats) --Will create the player's current XP
	exp.Name = "Current"

	local maxExp = Instance.new("IntValue", LevelStats) --Will create the player's Max XP they 
	maxExp.Name = "Max"

    local data
	--DataStore Load
    local success, whoops = pcall(function()
	    data = datastore:GetAsync(player.UserId)
    end)
	if success and data then
		level.Value = data["Level"]
		exp.Value = data["Current"]
		maxExp.Value = data["Max"]
	else
        warn(whoops)
		level.Value = 1
		exp.Value = 0
		maxExp.Value = 100
	end
end)

--DataStore Save
game.Players.PlayerRemoving:Connect(function(player)
	local level_stats = createTable(player)
    print(level_stats)
	local success, err = pcall(function()
		datastore:SetAsync(player.UserId, level_stats)
	end)

	if not success then 
		warn("Failed to save Level Stats!")
	end
end)

so it warned me with this instead of “whoops”
Screenshot 2021-05-30 180455
and the maxExp still was still 0, and nothing else on the output

Did level_stats print out a table of all the player’s stats? Or was that the only output message?

It might just be that you need to add a BindToClose() function so that you can try it inside Studio

it was the only output and imma try to add a BindToClose() function and see what happens

the max Exp value is still 0, I think I need to set the max Exp to 100 manually using the script i think using

if maxExp.Value == 0 then
    maxExp.Value == 100
end

I feel like adding this to the :PlayerAdded() function will fix the problem i am having hopefully.

it worked for a second and decided to break again.
Edit: The Current, and Level save but the MaxExp doesn’t

Can you send the updated code? I’ll see if I can find anything that may be causing MaxExp to not work.