Why is this at 0 when rejoining?

Yo

So I have a problem since a long time now.

Basically, I have made a DataStore system.
And whenever I rejoin, it works for every values EXCEPT MaxXP and XP and I have no clue why.

Another problem is that I am supposed to get 1 Level per kill, but it gives me 2.


This is the part of the code which saves the data when leaving.
As I mentionned above, everything works except these 2 values.

	local data

	local Success, ErrorMessage = pcall(function()
		data = DataStore:GetAsync(playerID)
	end)

	if Success then
		player.leaderstats.Yen.Value = data.Yen -- Saves
		player.leaderstats.Souls.Value = data.Souls -- Saves
		player.leaderstats.Level.Value = data.Level -- Saves
		player.leaderstats.Rebirths.Value = data.Rebirths -- Saves
		player.leaderstats.RebirthTokens.Value = data.RebirthTokens -- Saves
		player.leaderstats.RebirthYenBoost.Value = data.RebirthYenBoost -- Saves
		player.PetBoostMultiplier.Value = data.PetBoostMultiplier -- Saves
		player.leaderstats["Time Played"].Value = data.TimePlayed -- Saves
		
		player.Achievements.GoldenShurikens.Value = data.GoldenShurikens -- Saves
		
		player.Attributes.Health.Value = data.Health -- Saves
		player.Attributes.Speed.Value = data.Speed -- Saves
		player.Attributes.Attack.Value = data.Attack -- Saves
		player.Attributes.MaximumStamina.Value = data.MaximumStamina -- Saves
		
		player.Weapon.CurrentWeapon.Value = data.CurrentWeapon -- Saves
		
		player.leaderstats.Projectiles.Value = data.Projectiles -- Saves
		player.leaderstats.CurrentProjectile.Value = data.CurrentProjectile -- Saves
		
		player.QuestInformation.QuestTitle.Value = data.QuestTitle -- Saves
		player.QuestInformation.QuestTask.Value = data.QuestTask -- Saves
		player.QuestInformation.QuestObjective.Value = data.QuestObjective -- Saves
		player.QuestInformation.QuestProgress.Value = data.QuestProgress -- Saves
		player.QuestInformation.QuestReward.Value = data.QuestReward -- Saves
		player.QuestInformation.QuestReward2.Value = data.QuestReward2 -- Saves
		
		player.CustomizedCharacter.Value = data.CustomizedCharacter -- Saves
		player.HasReadTheTutorial.Value = data.HasReadTheTutorial -- Saves
		
		player.leaderstats.Level.MaxXP.Value = data.MaxXP
		player.leaderstats.Level.XP.Value = data.XP
		
	else
		print("There was an error whilst loading your data. We're sorry!")
		warn(ErrorMessage)
	end

I even tried printing the table and here is the result.

{Attack = 0, CurrentProjectile = "Shuriken", CurrentWeapon = "eeee", CustomizedCharacter = true, GoldenShurikens = 2, HasReadTheTutorial = true, Health = 0, Level = 98, MaxXP = 0, MaximumStamina = 0, PetBoostMultiplier = 3, Projectiles = 90, QuestObjective = 0, QuestProgress = 0, QuestReward = 0, QuestReward2 = 0, QuestTask = "Default", QuestTitle = "Default", RebirthTokens = 100, RebirthYenBoost = 100, Rebirths = 100, Souls = 76, Speed = 0, TimePlayed = 7945, XP = 0, Yen = 7549100}

Fixing this will save my entire game, so please help lol

Thanks, bye!

Most likely you change xp and MaxXp on the client, which leads to it being 0 on the server

2 Likes

Can you show me in what script, and how you are saving it?

1 Like

I am changing it on the server. So there’s no problems with that.

Here is my entire leaderstats script. You will find the DataStore part if you scroll down quite a bit.

 -- DataStore Variables

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
local ToolsDataStore = DataStoreService:GetDataStore("ToolsDataStore")

----------------------------------------------

-- Variables

local TablePrinterModule = require(3148021300)

----------------------------------------------

-- Attribute Variables

local StarterSpeed = game:GetService("ReplicatedStorage"):WaitForChild("CurrentWalkSpeed").Value
local StarterJumpPower = 50
local starterMaxStamina = 100

local spdPerPoint = 0.5
local hpPerPoint = 10
local jumpPowerPerPoint = 1
local maxStaminaPerPoint = 10

local speedCap = 80
local jumpCap = 120

local HealthIncrementPerLevel = 5

----------------------------------------------

-- Cancel Quest
local remote = game:GetService("ReplicatedStorage"):WaitForChild("QuestRemote")

remote.OnServerEvent:Connect(function(player, val)
	local questInfoF = player:WaitForChild("QuestInformation")
	if val == "CancelQuest" then
		for i, v in pairs(questInfoF:GetChildren()) do
			if v.className == "StringValue" then
				v.Value = "Default"
			elseif v.className == "IntValue" then
				v.Value = 0
			end
		end
	end
end)

----------------------------------------------------------------------------------------------

game.Players.PlayerAdded:Connect(function(player)
	
	-- Music
	player.PlayerGui:WaitForChild("GameMusic"):Play()

	-----------------
	
	-- Some Variables
	
	local playerID = player.UserId
	
	local character = player.Character
	
	-----------------
		
	-- Folders
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local weapon = Instance.new("Folder", player)
	weapon.Name = "Weapon"
	
	local attacks = Instance.new("Folder", weapon)
	attacks.Name = "WeaponAttacks"

	local projectiles = Instance.new("IntValue", leaderstats)
	projectiles.Name = "Projectiles"
	projectiles.Value = 100

	local currentProjectile = Instance.new("StringValue", leaderstats)
	currentProjectile.Name = "CurrentProjectile"
	currentProjectile.Value = "Shuriken"

	local QuestInformation = Instance.new("Folder", player)
	QuestInformation.Name = "QuestInformation"
	
	local attributes = Instance.new("Folder")
	attributes.Name = "Attributes"
	attributes.Parent = player
	
	local petBoostMultiplier = Instance.new("NumberValue", player)
	petBoostMultiplier.Name = "PetBoostMultiplier"
	petBoostMultiplier.Value = 1

	local hasReadTheTutorial = Instance.new("BoolValue", player)
	hasReadTheTutorial.Name = "HasReadTheTutorial"
	hasReadTheTutorial.Value = false

	local customizedCharacter = Instance.new("BoolValue", player)
	customizedCharacter.Name = "CustomizedCharacter"
	customizedCharacter.Value = false
	
	local loadingScreen = Instance.new("BoolValue", player)
	loadingScreen.Name = "LoadingScreen"
	loadingScreen.Value = false
	
	-----------------
	
	-- leaderstats
	local yen = Instance.new("IntValue", leaderstats)
	yen.Name = "Yen"
	
	local soulPoints = Instance.new("IntValue", leaderstats)
	soulPoints.Name = "Souls"

	local Level = Instance.new("IntValue", leaderstats)
	Level.Name = "Level"
	Level.Value = 1
	
	local XP = Instance.new("IntValue", Level)
	XP.Name = "XP"
	
	local MaxXP = Instance.new("IntValue", Level)
	MaxXP.Name = "MaxXP"
	
	local rebirths = Instance.new("IntValue", leaderstats)
	rebirths.Name = "Rebirths"
	rebirths.Value = 0
	
	local rebirthTokens = Instance.new("IntValue", leaderstats)
	rebirthTokens.Name = "RebirthTokens"
	rebirthTokens.Value = 0
	
	local rebirthYenBoost = Instance.new("IntValue", leaderstats)
	rebirthYenBoost.Name = "RebirthYenBoost"
	rebirthYenBoost.Value = 1
	
	local stamina = Instance.new("IntValue", leaderstats)
	stamina.Name = "Stamina"
	stamina.Value = 100
	
	local maxStamina = Instance.new("IntValue", stamina)
	maxStamina.Name = "MaxStamina"
	maxStamina.Value = 100
	
	local timePlayed = Instance.new("IntValue", leaderstats)
	timePlayed.Name = "Time Played"
	timePlayed.Value = 0
	
	-----------------

	-- Achievements
	local achievements = Instance.new("Folder", player)
	achievements.Name = "Achievements"
	
	local goldenShurikens = Instance.new("IntValue", achievements)
	goldenShurikens.Name = "GoldenShurikens"
	goldenShurikens.Value = 0
	
	-----------------
	
	-- Weapon
	local currentWeapon = Instance.new("StringValue", weapon)
	currentWeapon.Name = "CurrentWeapon"
	currentWeapon.Value = ""
	
	----------------
	
	-- QuestInfo
	local Title = Instance.new("StringValue", QuestInformation)
	Title.Name = "QuestTitle"
	Title.Value = "Default"

	local Task = Instance.new("StringValue", QuestInformation)
	Task.Name = "QuestTask"
	Task.Value = "Default"

	local Objective = Instance.new("IntValue", QuestInformation)
	Objective.Name = "QuestObjective"
	Objective.Value = 0

	local Progress = Instance.new("IntValue", QuestInformation)
	Progress.Name = "QuestProgress"
	Progress.Value = 0

	local Reward = Instance.new("IntValue", QuestInformation)
	Reward.Name = "QuestReward"
	Reward.Value = 0

	local Reward2 = Instance.new("IntValue", QuestInformation)
	Reward2.Name = "QuestReward2"
	Reward2.Value = 0
	
	-----------------
	
	-- IsUsingAttack Values
	local IsUsingAttack1 = Instance.new("BoolValue", attacks)
	IsUsingAttack1.Name = "IsUsingAttack1"
	IsUsingAttack1.Value = false
	
	local IsUsingAttack2 = Instance.new("BoolValue", attacks)
	IsUsingAttack2.Name = "IsUsingAttack2"
	IsUsingAttack2.Value = false
	
	local IsUsingAttack3 = Instance.new("BoolValue", attacks)
	IsUsingAttack3.Name = "IsUsingAttack3"
	IsUsingAttack3.Value = false
	
	local IsUsingAttack4 = Instance.new("BoolValue", attacks)
	IsUsingAttack4.Name = "IsUsingAttack4"
	IsUsingAttack4.Value = false
	
	local IsUsingAttack5 = Instance.new("BoolValue", attacks)
	IsUsingAttack5.Name = "IsUsingAttack5"
	IsUsingAttack5.Value = false
	
	local IsUsingAttack6 = Instance.new("BoolValue", attacks)
	IsUsingAttack6.Name = "IsUsingAttack6"
	IsUsingAttack6.Value = false

	local IsUsingAttack7 = Instance.new("BoolValue", attacks)
	IsUsingAttack7.Name = "IsUsingAttack7"
	IsUsingAttack7.Value = false

	-----------------
	
	-- Attributes
	local health = Instance.new("IntValue")
	health.Name = "Health"
	health.Parent = attributes
	
	local attack = Instance.new("IntValue")
	attack.Name = "Attack"
	attack.Parent = attributes
	
	local speed = Instance.new("IntValue")
	speed.Name = "Speed"
	speed.Parent = attributes
	
	local maximumStamina = Instance.new("IntValue")
	maximumStamina.Name = "MaximumStamina"
	maximumStamina.Parent = attributes
	
	-----------------
	
	-- Player's Weapon Names
	
	local WeaponNames = Instance.new("Folder", player)
	WeaponNames.Name = "WeaponNames"
	
	-----------------
	
	-- Level Up

	XP:GetPropertyChangedSignal('Value'):Connect(function()
		if XP.Value >= MaxXP.Value then
			Level.Value += 1
			XP.Value = 0
			MaxXP.Value *= 1.25
			character.Humanoid.MaxHealth += HealthIncrementPerLevel
		end
	end)
	
	-----------------

	-- Attribute Points

	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local hpRemote = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("AddHealth")
	local spdRemote = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("AddSpeed")
	local atkRemote = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("AddAttack")
	local maxStaminaRemote = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("AddMaxStamina")
	local reset = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("Reset")


	local function PointsAvailable(player)
		local PointsUsed = player.Attributes.Health.Value + player.Attributes.Speed.Value + player.Attributes.Attack.Value + player.Attributes.MaxStamina.Value
		local PointsAvailable = player.leaderstats.Level.Value - PointsUsed
		return PointsAvailable > 0
	end

	hpRemote.OnServerEvent:Connect(function(player)
		player.Attributes.Health.Value += 1
		player.Character.Humanoid.MaxHealth = 100 + player.Attributes.Health.Value * hpPerPoint
		player.Character.Humanoid.Health = 100 + player.Attributes.Health.Value * hpPerPoint
	end)

	spdRemote.OnServerEvent:Connect(function(player)
		if player.Attributes.Speed.Value < speedCap then
			player.Attributes.Speed.Value += 1
			player.Character.Humanoid.WalkSpeed = StarterSpeed + player.Attributes.Speed.Value * spdPerPoint
		end
	end)

	atkRemote.OnServerEvent:Connect(function(player)
		player.Attributes.Attack.Value += 1
	end)

	maxStaminaRemote.OnServerEvent:Connect(function(player)
		player.Attributes.MaximumStamina.Value += 1
		player.leaderstats.Stamina.MaxStamina.Value += maxStaminaPerPoint
	end)

	reset.OnServerEvent:Connect(function(player)
		speed.Value = 0
		attack.Value = 0
		health.Value = 0
		maximumStamina.Value = 0
		player.Character.Humanoid.MaxHealth = 100 + player.Attributes.Health.Value * hpPerPoint
		player.Character.Humanoid.Health = 100 + player.Attributes.Health.Value * hpPerPoint
		player.Character.Humanoid.WalkSpeed = StarterSpeed + player.Attributes.Speed.Value * spdPerPoint
		maxStamina.Value = starterMaxStamina
	end)
	-----------------
	
	-- Quest Reward
	local rewarded = false

	Title.Changed:Connect(function()
			Progress.Changed:Connect(function()
			if Progress.Value >= Objective.Value and rewarded == false then
				rewarded = true
				local function GiveReward()
					player:WaitForChild("leaderstats"):WaitForChild("Yen").Value += Reward.Value
					player:WaitForChild("leaderstats"):WaitForChild("Level"):WaitForChild("XP").Value += Reward2.Value
					Title.Value = "Default"
					Task.Value = "Default"
					Progress.Value = 0
					Objective.Value = 0
					Reward.Value = 0
					Reward2.Value = 0
					player:WaitForChild("PlayerGui"):WaitForChild("Notifications"):WaitForChild("CompletedQuest").Visible = true
					wait(2)
					player:WaitForChild("PlayerGui"):WaitForChild("Notifications"):WaitForChild("CompletedQuest").Visible = false
				end
				GiveReward()
				wait(0.001)
				remote:FireClient(player, "CancelQuest")
				wait(0.5)
				rewarded = false
			end
		end)
	end)
	
	-----------------

	-- DataStore

	local data

	local Success, ErrorMessage = pcall(function()
		data = DataStore:GetAsync(playerID)
	end)

	if Success then
		player.leaderstats.Yen.Value = data.Yen -- Saves
		player.leaderstats.Souls.Value = data.Souls -- Saves
		player.leaderstats.Level.Value = data.Level -- Saves
		player.leaderstats.Rebirths.Value = data.Rebirths -- Saves
		player.leaderstats.RebirthTokens.Value = data.RebirthTokens -- Saves
		player.leaderstats.RebirthYenBoost.Value = data.RebirthYenBoost -- Saves
		player.PetBoostMultiplier.Value = data.PetBoostMultiplier -- Saves
		player.leaderstats["Time Played"].Value = data.TimePlayed -- Saves
		
		player.Achievements.GoldenShurikens.Value = data.GoldenShurikens -- Saves
		
		player.Attributes.Health.Value = data.Health -- Saves
		player.Attributes.Speed.Value = data.Speed -- Saves
		player.Attributes.Attack.Value = data.Attack -- Saves
		player.Attributes.MaximumStamina.Value = data.MaximumStamina -- Saves
		
		player.Weapon.CurrentWeapon.Value = data.CurrentWeapon -- Saves
		
		player.leaderstats.Projectiles.Value = data.Projectiles -- Saves
		player.leaderstats.CurrentProjectile.Value = data.CurrentProjectile -- Saves
		
		player.QuestInformation.QuestTitle.Value = data.QuestTitle -- Saves
		player.QuestInformation.QuestTask.Value = data.QuestTask -- Saves
		player.QuestInformation.QuestObjective.Value = data.QuestObjective -- Saves
		player.QuestInformation.QuestProgress.Value = data.QuestProgress -- Saves
		player.QuestInformation.QuestReward.Value = data.QuestReward -- Saves
		player.QuestInformation.QuestReward2.Value = data.QuestReward2 -- Saves
		
		player.CustomizedCharacter.Value = data.CustomizedCharacter -- Saves
		player.HasReadTheTutorial.Value = data.HasReadTheTutorial -- Saves
		
		player.leaderstats.Level.MaxXP.Value = data.MaxXP
		player.leaderstats.Level.XP.Value = data.XP
		
	else
		print("There was an error whilst loading your data. We're sorry!")
		warn(ErrorMessage)
	end
	
	local ToolData = ToolsDataStore:GetAsync(player.UserId)
	local WeaponsFolder = game:GetService("ServerStorage"):WaitForChild("Weapons")
		
	if ToolData then
		for i, tool in pairs(ToolData) do
			local FolderName = tool:FindFirstChild("SaveToolSystem").FolderName.Value
			local ClonedTool = WeaponsFolder:FindFirstChild(FolderName):FindFirstChild(tool):Clone()
			ClonedTool.Parent = player.Backpack
		end
	end
end)

-------------------------------------------------------------------

-- Save Player's Data

game.Players.PlayerRemoving:Connect(function(player)
	
	local Data = {	
		Yen = player.leaderstats.Yen.Value, -- Saves
		Souls = player.leaderstats.Souls.Value, -- Saves
		Level = player.leaderstats.Level.Value, -- Saves
		Rebirths = player.leaderstats.Rebirths.Value, -- Saves
		RebirthTokens = player.leaderstats.RebirthTokens.Value, -- Saves
		RebirthYenBoost = player.leaderstats.RebirthYenBoost.Value, -- Saves
		PetBoostMultiplier = player.PetBoostMultiplier.Value, -- Saves
		TimePlayed = player.leaderstats["Time Played"].Value, -- Saves
		
		GoldenShurikens = player.Achievements.GoldenShurikens.Value, -- Saves
		
		Health = player.Attributes.Health.Value, -- Saves
		Speed = player.Attributes.Speed.Value, -- Saves
		Attack = player.Attributes.Attack.Value, -- Saves
		MaximumStamina = player.Attributes.MaximumStamina.Value, -- Saves
		
		CurrentWeapon = player.Weapon.CurrentWeapon.Value, -- Saves
		
		Projectiles = player.leaderstats.Projectiles.Value, -- Saves
		CurrentProjectile = player.leaderstats.CurrentProjectile.Value, -- Saves
		
		QuestTitle = player.QuestInformation.QuestTitle.Value, -- Saves
		QuestTask = player.QuestInformation.QuestTask.Value, -- Saves
		QuestObjective = player.QuestInformation.QuestObjective.Value, -- Saves
		QuestProgress = player.QuestInformation.QuestProgress.Value, -- Saves
		QuestReward = player.QuestInformation.QuestReward.Value, -- Saves
		QuestReward2 = player.QuestInformation.QuestReward2.Value, -- Saves
		
		CustomizedCharacter = player.CustomizedCharacter.Value, -- Saves
		HasReadTheTutorial = player.HasReadTheTutorial.Value, -- Saves
		
		MaxXP = player.leaderstats.Level.MaxXP.Value,
		XP = player.leaderstats.Level.XP.Value
	}

	local ToolsData = {}
	
--[[	for i, tool in pairs(player.Character:GetChildren()) do
		if tool:IsA("Tool") then
			table.insert(ToolsData, tool.Name)
		end
	end

	for i, tool in pairs(player.Backpack:GetChildren()) do
		if tool:IsA("Tool") then
			table.insert(ToolsData, tool.Name)
		end
	end			]]

	local Success, ErrorMessage = pcall(function()
		DataStore:SetAsync(player.UserId, Data)
		ToolsDataStore:SetAsync(player.UserId, ToolsData)
	end)

	if Success then
		print("Data successfully saved!")
		print(TablePrinterModule(Data))
	else
		print("There was an error whilst loading your data. We're sorry!")
		warn(ErrorMessage)
	end
	
end)

------------

-- DataStore in-studio

game:BindToClose(function()
	if game:GetService('RunService'):IsStudio() then
		wait(2)
	end
end)

I don’t see anything wrong with the script. I also know you aren’t hitting the character limit. I am sorry I can’t help.

1 Like
  1. Maybe you should inserting XP and MaxXP into leaderstats not Level
  2. Check if datastore is really saving. (Check in roblox not in studio) if really saves then check into loading to the player
  3. About your Level rankup by 2, check for rewards. (For that need more code to see)
1 Like
  1. No I didn’t, I checked everything.
  2. It is saving. As I mentionned above, everything saves except those 2 specific values + I do already test in-game.
  3. As I mentionned above (again), the code is supposed to give 1 Level.

Nothing helps yet, sorry.

1 Like

Are you getting tht print during GetAsync? If yes thhen try printing the Table being saved.

1 Like

I’m getting that print during the SetAsync. When the player is removed (when he’s leaving)

Okay nevermind, you do really help, I got it working.
Thank you so much, you saved my game!!


For the ones who have this issue and who are watching this post in the future, the issue was that the XP and MaxXP parents were the Level. So replace the parent from Level to leaderstats.

2 Likes