Data Store issue: 'level' and 'required xp' value are unable to load properly

Hi! I have a data store script which shows the following values:

"Coins"
"Wins"
"Visits"
"R$ Donated"
"Level"
"XP"
"RequiredXP"

What is the problem?

Script:

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

local badge = game:GetService("BadgeService")

local function saveData(player)

	local tableToSave = {
		player.leaderstats.Coins.Value;
		player.leaderstats.Wins.Value;
		player.Visits.Value;
		player["R$ Donated"].Value;
		player.leaderstats.Level.Value;
		player.XP.Value;
		player.RequiredXP.Value;
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave)
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data has not been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 0
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	Wins.Value = 0
	
	local Visits = Instance.new("IntValue")
	Visits.Name = "Visits"
	Visits.Parent = player
	Visits.Value = 1
	
	local Donated = Instance.new("IntValue")
	Donated.Name = "R$ Donated"
	Donated.Parent = player
	Donated.Value = 0
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats
	level.Value = 1

	local XP = Instance.new("NumberValue")
	XP.Name = "XP"
	XP.Parent = player
	XP.Value = 0

	local requiredXP = Instance.new("NumberValue")
	requiredXP.Name = "RequiredXP"
	requiredXP.Parent = player
	requiredXP.Value = level.Value*25
	
	XP.Changed:Connect(function()
		if XP.Value >= requiredXP.Value then
			requiredXP.Value = level.Value*25
			level.Value += 1
			XP.Value = 0
		end
	end)
	
	Wins.Changed:Connect(function()
		
		if player.leaderstats.Wins.Value == 1 then

			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127079658)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end

		if player.leaderstats.Wins.Value == 2 then

			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127079679)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end
		
	end)
	
	Visits.Changed:Connect(function()
		
		if Visits.Value == 10 then
			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127209356)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end
		
		if Visits.Value == 25 then
			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127209520)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end
		
		if Visits.Value == 50 then
			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127209540)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end
		
	end)

	local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)

	if success and data then

		Coins.Value = data[1]
		Wins.Value = data[2]
		Visits.Value = data[3]
		Visits.Value += 1
		Donated.Value = data[4]
		level.Value = data[5]
		XP.Value = data[6]
		requiredXP.Value = data[7]
		
	else
		print("The player has no data!")
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err  = pcall(function()
		saveData(player)
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data has not been saved!")
		warn(err)
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player)
		end)

		if success then
			print("Data has been saved!")
		else
			print("Data has not been saved!")
			warn(err)
		end
	end
end)

There is an issue with my Level, XP and RequiredXP value. It’s supposed to be set to 1 but the Level and RequiredXP are both set to zero.

Please help me fix this. Thanks.

Edit: Forgot to mention that there are no errors or warnings when I run the script.

try printing the data you’re reading from (when players join) to verify the table is correct

Why are you saving the required xp? after all it’s just the level * 25, i don’t see a need to save it.

Printed the data, I still got zero.
Screenshot (147)

I mean after this do print(data) so you get the raw table your saving from

also for bonus points, after you create tableToSave, print(tableToSave)

this narrows down whether its an issue with saving or loading

1 Like

I don’t understand, it works for me, my level is set to 1 and my requiredXP is set to 25, as for the XP it is set to 0 but that’s the default value you set.

image

local XP = Instance.new("NumberValue")
XP.Name = "XP"
XP.Parent = player
XP.Value = 0 --default value is 0

And yes, this is after rejoining with data and access to api enabled.

1 Like

I see that it works for you but…
Screenshot (148)

My guess is that you have corrupted data from when you were testing an unfinished datastore code, just change the data store

local dataStore = DataStoreService:GetDataStore("MyDataStore2") --change to a new data store

Also please don’t save requiredXP if possible, as there is no need, just grab the player’s level and multiply it by 25 and there you go.

Thank you! It’s working fine now.

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