DataStore Script not working

Hello!
So i’m currently working on a Data Save System but when I test the game and then leave, it does not save the data. Any idea?


This is the code to load the data :

local Data

local Success, ErrorMessage = pcall(function()
	Data = DataStore:GetAsync(player.UserId.."-data")
end)

if Success then
	player.leaderstats.Yen.Value = Data.Yen
    player.leaderstats.Souls.Value = Data.Souls
else
	print("There was an error whilst loading your data. We're sorry!")
	warn(ErrorMessage)
end

This is the code to give the data back to the player :

game.Players.PlayerRemoving:Connect(function(player)
	
	local playerID = "Player_"..player.UserId
	
	local Data = {	
		Yen = player.leaderstats.Yen.Value,
		Souls = player.leaderstats.Souls.Value
	}

	
	local Success, ErrorMessage = pcall(function()
		DataStore:SetAsync(playerID, 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)

These are different keys, they must be the same…

1 Like

Unfortunately I changed both to the same, but it still doesn’t save.

Any error messages? Could you maybe provide your entire code?

1 Like

Nope, no error messages. It’s also printing "Data successfully saved!" but it doesn’t.

This is my entire leaderstats,
it’s 400 lines but it should help you find out.

I’ve also realised that it breaks the leaderstats because of the Save System.

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

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

-- 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 = "Poseidon"
	
	----------------
	
	-- 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
	
	-----------------

	-- 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)

	-----------------
	
	-- Level Up
	
	while wait(0.1) do
		if XP.Value >= MaxXP.Value then
			Level.Value = Level.Value + 1
			XP.Value = 0
			MaxXP.Value = MaxXP.Value * 1.5
		end
	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
	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,
		
		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)

Are you testing in studio?
{ignore this}

1 Like

I was testing in studio before but learnt that it wouldn’t work in studio so now i’m testing in an actual server.

In Studio, turn on the setting “Enable Studio Access to API Services”.

1 Like

API Services doesn’t seem to be the issue since it’s already enabled

I found the Issue.

Your Including A while Loop inside the Player, This will make all the code below the loop to not run.
That means the code after the while wait(0.1) do will NOT Run.

A Simple Solution is to Not Use Loops and Instead Run the Level Changing Function When the Player’s XP Is Changed. We can Use :GetPropertyChangedSignal For this.

Replace that Part with

	-- Level Up
	
	XP:GetPropertyChangedSignal('Value'):Connect(function()
		if XP.Value >= MaxXP.Value then
			Level.Value += 1
			XP.Value = 0
			MaxXP.Value = MaxXP.Value * 1.5
		end
	end)

Also, at the End of your Code, Put

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

This will make it so that you can test your data saving in studio instead of in-game.

2 Likes

Absolutely EVERYTHING works perfectly now. Thanks for helping me @WheezWasTaken you are amazing!

Thanks to @Vaschex either since he also helped me! :smiley: