Problems with DataSave System

Introduction

Hey,
I have a few problems which are very strange with my DataSave System.


My Code (leaderstats)

-- DataStore Variables

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

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

-- Attribute Variables

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

local spdPerPoint = 10
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"
	yen.Value = 0
	
	local soulPoints = Instance.new("IntValue", leaderstats)
	soulPoints.Name = "Souls"
	soulPoints.Value = 0

	local Level = Instance.new("IntValue", leaderstats)
	Level.Name = "Level"
	Level.Value = 1
	
	local XP = Instance.new("IntValue", Level)
	XP.Name = "XP"
	XP.Value = 0
	
	local MaxXP = Instance.new("IntValue", Level)
	MaxXP.Name = "MaxXP"
	MaxXP.Value = 100
	
	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

	-----------------
	
	-- 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
	
	-----------------
	
	-- Level Up

	XP:GetPropertyChangedSignal('Value'):Connect(function()
		if XP.Value >= MaxXP.Value then
			Level.Value = Level.Value + 1
			XP.Value = 0
			MaxXP.Value = MaxXP.Value * 1.5
			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
				if Title.Value == "Casual Fighters" then
					player:WaitForChild("leaderstats"):WaitForChild("Yen").Value += Reward.Value
					player:WaitForChild("leaderstats"):WaitForChild("Level"):WaitForChild("XP").Value += Reward2.Value
					player:WaitForChild("PlayerGui"):WaitForChild("Notifications"):WaitForChild("CompletedQuest").Visible = true
					wait(2)
					player:WaitForChild("PlayerGui"):WaitForChild("Notifications"):WaitForChild("CompletedQuest").Visible = false
				end
				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
		player.leaderstats.Souls.Value = Data.Souls
		player.leaderstats.Level.Value = Data.Level
		player.leaderstats.Level.XP.Value = Data.XP
		player.leaderstats.Level.MaxXP.Value = Data.MaxXP
		player.leaderstats.Rebirths.Value = Data.Rebirths
		player.leaderstats.RebirthTokens.Value = Data.RebirthTokens
		player.leaderstats.RebirthYenBoost.Value = Data.RebirthYenBoost
		player.PetBoostMultiplier.Value = Data.PetBoostMultiplier
		player.leaderstats["Time Played"].Value = Data.TimePlayed
		
		player.Achievements.GoldenShurikens.Value = Data.GoldenShurikens
		
		player.Attributes.Health.Value = Data.Health
		player.Attributes.Speed.Value = Data.Speed
		player.Attributes.Attack.Value = Data.Attack
		player.Attributes.MaximumStamina.Value = Data.MaximumStamina
		
		player.Weapon.CurrentWeapon.Value = Data.CurrentWeapon
		
		player.QuestInformation.QuestTitle.Value = Data.QuestTitle
		player.QuestInformation.QuestTask.Value = Data.QuestTask
		player.QuestInformation.QuestObjective.Value = Data.QuestObjective
		player.QuestInformation.QuestProgress.Value = Data.QuestProgress
		player.QuestInformation.QuestReward.Value = Data.QuestReward
		player.QuestInformation.QuestReward2.Value = Data.QuestReward2
		
		player.CustomizedCharacter.Value = Data.CustomizedCharacter
		player.HasReadTheTutorial.Value = Data.HasReadTheTutorial
	else
		print("There was an error whilst loading your data. We're sorry!")
		warn(ErrorMessage)
	end
	
	
end)

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

-- Save Player's Data

game.Players.PlayerRemoving:Connect(function(player)
	
	local Data = {
		
		Yen = player.leaderstats.Yen.Value,
		Souls = player.leaderstats.Souls.Value,
		Level = player.leaderstats.Level.Value,
		XP = player.leaderstats.Level.XP.Value,
		MaxXP = player.leaderstats.Level.MaxXP.Value,
		Rebirths = player.leaderstats.Rebirths.Value,
		RebirthTokens = player.leaderstats.RebirthTokens.Value,
		RebirthYenBoost = player.leaderstats.RebirthYenBoost.Value,
		PetBoostMultiplier = player.PetBoostMultiplier.Value,
		TimePlayed = player.leaderstats["Time Played"].Value,
		
		GoldenShurikens = player.Achievements.GoldenShurikens.Value,
		
		Health = player.Attributes.Health.Value,
		Speed = player.Attributes.Speed.Value,
		Attack = player.Attributes.Attack.Value,
		Stamina = player.Attributes.MaximumStamina.Value,
		
		CurrentWeapon = player.Weapon.CurrentWeapon.Value,
		
		Projectiles = player.leaderstats.Projectiles.Value,
		CurrentProjectile = player.leaderstats.CurrentProjectile.Value,
		
		QuestTitle = player.QuestInformation.QuestTitle.Value,
		QuestTask = player.QuestInformation.QuestTask.Value,
		QuestObjective = player.QuestInformation.QuestObjective.Value,
		QuestProgress = player.QuestInformation.QuestProgress.Value,
		QuestReward = player.QuestInformation.QuestReward.Value,
		QuestReward2 = player.QuestInformation.QuestReward2.Value,
		
		CustomizedCharacter = player.CustomizedCharacter.Value,
		HasReadTheTutorial = player.HasReadTheTutorial.Value
		}
	
	local Success, ErrorMessage = pcall(function()
		DataStore:SetAsync(player.UserId, Data)
	end)
	
	if Success then
		print("Data successfully saved!")
	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(1)
	end
end)

Problems

  • When leaving, it only saves Yen and Souls but not the rest of the values
  • Once rejoining, I get 2 Levels when killing mobs instead of 1
Mob XP Code
local Humanoid = script.Parent:FindFirstChildWhichIsA("Humanoid")

local Settings = script.Parent:WaitForChild("Settings")
local SoulsToGive = Settings.Souls.Value
local YenToGive = Settings.Yen.Value
local XPToGive = Settings.XP.Value

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

function Dead()
	
	local tag = Humanoid:FindFirstChild("creator")
	
	if tag ~= nil then
			
			local leaderstats = tag.Value:WaitForChild("leaderstats")
			local Level = leaderstats:WaitForChild("Level")
			local Current = Level:WaitForChild("XP")
			local Yen = leaderstats:WaitForChild("Yen")
			local Souls = leaderstats:WaitForChild("Souls")

			Current.Value += XPToGive
			Yen.Value += YenToGive * tag.Value:WaitForChild("PetBoostMultiplier").Value * leaderstats:WaitForChild("RebirthYenBoost").Value
			Souls.Value += SoulsToGive
			wait(0.1)
			script:Remove()

		end
	end

Humanoid.Died:Connect(Dead)
  • When rejoining, the GUI says I have 0 Yen but printing says otherwise

Outroduction

Any help is appreciated!
Note: Please don’t feel rushed when helping and take your time :slight_smile:

1 Like

I took a look at your script, regarding the first issue, everything that is shown here is saved for me.


This means that the “Projectiles” value is saved but not loaded.
Now whilst I can’t figure out why the second problem occurs, I do see an issue
image

image

image

In the above images, you are setting the changing the MaxXP value to 100 then running the function to add 1 to the player’s level if they have more XP than MaxXP. After that, on line 337, you are changing the player XP value first, which then triggers the above mentioned function to change the player’s level before their MaxXP loads. What this does, if the player has more than 100 XP when they leave, the next time they join, they will get an instant level-up.

Another thing that I would advise against doing is using :Remove() which has been deprecated in favor of :Destroy().

2 Likes

Where should I locate the script:Remove() in my script?

In the second script you sent, replace :Remove() with :Destroy(), it won’t fix it, but just something you should do.

1 Like