My leaderstats not saving

My leaderstats not saving when leaving

Before

After

my code:

local DataStore2 = require(game.ServerStorage:WaitForChild(“DataStore2”))
local DEFAULTS = 15
local DefaultRank = “Noob”
local DefaultRebirthsValue = 1

function playerAdded(player)
local blades = DataStore2(“blades”, player)
local bladesSt = DataStore2(“bladesSt”, player)
local coins = DataStore2(“coins”, player)
local Ranks = DataStore2(“Ranks”, player)
local RebirthValues = DataStore2(“RebirthsValue”, player)
local FullSignal = DataStore2(“FullSignal”, player)

local Fold = Instance.new("Folder")
Fold.Name = "leaderstats"
Fold.Parent = player

local Blades = Instance.new("IntValue")
Blades.Name = "Blades"
Blades.Value = blades:Get(0)  -- there's no need for a function, you can hook to a getpropertychangedsignal on the client
-- the only reason why the docs have a function it to get changes
Blades.Parent = Fold

blades:OnUpdate(function(value) -- hook to onupdate, because when you update with datastore2 you use functional methods, you don't update value objects
	-- you would use methods like :Increment(-value) when purchasing items (you lose money when you buy stuff)
	Blades.Value = value
end)

local BladesSt = Instance.new("IntValue")
BladesSt.Name = "BladesSt"
BladesSt.Value = bladesSt:Get(DEFAULTS)
BladesSt.Parent = Blades -- Why is it originally inside blades?

bladesSt:OnUpdate(function(value)
	BladesSt.Value = value
end)

local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = coins:Get(0)
Coins.Parent = Fold

coins:OnUpdate(function(value)
	Coins.Value = value
end)

local Rank = Instance.new("StringValue")
Rank.Name = "Class"
Rank.Value = Ranks:Get(DefaultRank)
Rank.Parent = Fold

Ranks:OnUpdate(function(value)
	Rank.Value = value
end)

local ClassVals = Instance.new("IntValue")
ClassVals.Name = "ClassValues"
ClassVals.Value = RebirthValues:Get(DefaultRebirthsValue)
ClassVals.Parent = Rank

RebirthValues:OnUpdate(function(value)
	ClassVals.Value = value
end)

local Full = Instance.new("BoolValue")
Full.Name = "Full"
Full.Value = FullSignal:Get(false)
Full.Parent = Blades

FullSignal:OnUpdate(function(value)
	Full.Value = value
end)

end

for _, player in pairs(game.Players:GetPlayers()) do
playerAdded(player)
end

game.Players.PlayerAdded:Connect(playerAdded)

add points where the data does not load anymore when i use it

local player = game.Players.LocalPlayer
local RepStorage = game.ReplicatedStorage
local Events = RepStorage.Events – remember to cal event or nil
local CoinClicked = Events.CoinClicked
– Finding The Vals
local Settings = script.Parent.Settings
local Strength = Settings.Strength
local tool = script.Parent
local Debounce = false

tool.Activated:Connect(function()
if player.leaderstats.Blades.Value < player.leaderstats.Blades.BladesSt.Value then
if Debounce == false then
wait(2.5)

		workspace.AddPoints.AddPointsEvent:FireServer()
		
		wait(.2)
		
		local Debounce = true
		
		wait(4.5)
		
		local Debounce = false
		
	elseif player.leaderstats.Blades.Value == player.leaderstats.Blades.BladesSt.Value then
		script.Disabled = true
	else
		script.Disabled = false
	end
end

end)

Help is needed if not i try to solve this my own

1 Like

I’ve been having this problem too, except I use Data store. It might be a bug?

Please fix the formatting of your code samples. It makes it significantly difficult to address your code when thread details are merged with your code samples.

The best way to ensure a code sample makes it in properly is to use the three back ticks ``` method above and below your code instead of posting the script straight up onto the thread.

```lua
Code
```

In addition, as per your last comment, you should have tried figuring this out on your own before posting a thread. Apply basic debugging and make sure both your code and tests aren’t going off the wrong way.

By the way, I notice that you’re opening up 6 DataStores and lacking use of Combine. To my knowledge, per DataStore2, there are two DataStore requests submitted: one for an OrderedDataStore and one for a regular DataStore. 6 DataStore2s means 12 requests per player, which will quickly exhaust the per minute budget request amount. It already eats past the per-player increment and into the server minimums.

I am sure you are not using OnUpdate function correctly in all datastores.
For example on one of the datastore:

Instead of:

blades:OnUpdate(function(value) 
	Blades.Value = value
end)

It should be:

blades:OnUpdate(function(value) 
	Blades.Value = blades:Get(value)
end)

This should be done on all others datastores as well.

@colbert2677 when I check my code there is nothing wrong with it before I add my tool when I added my tool the datastores have been broke

Would it lemme try later can’t use studio yet

There is nothing wrong with your code, but it is very messy to read, and hard to copy. Please make it one scrollable block of code.

1 Like
local DataStore2 = require(game.ServerStorage:WaitForChild(“DataStore2”))
local DEFAULTS = 15
local DefaultRank = “Noob”
local DefaultRebirthsValue = 1

function playerAdded(player)
local blades = DataStore2(“blades”, player)
local bladesSt = DataStore2(“bladesSt”, player)
local coins = DataStore2(“coins”, player)
local Ranks = DataStore2(“Ranks”, player)
local RebirthValues = DataStore2(“RebirthsValue”, player)
local FullSignal = DataStore2(“FullSignal”, player)

local Fold = Instance.new("Folder")
Fold.Name = "leaderstats"
Fold.Parent = player

local Blades = Instance.new("IntValue")
Blades.Name = "Blades"
Blades.Value = blades:Get(0)  -- there's no need for a function, you can hook to a getpropertychangedsignal on the client
-- the only reason why the docs have a function it to get changes
Blades.Parent = Fold

blades:OnUpdate(function(value) -- hook to onupdate, because when you update with datastore2 you use functional methods, you don't update value objects
	-- you would use methods like :Increment(-value) when purchasing items (you lose money when you buy stuff)
	Blades.Value = value
end)

local BladesSt = Instance.new("IntValue")
BladesSt.Name = "BladesSt"
BladesSt.Value = bladesSt:Get(DEFAULTS)
BladesSt.Parent = Blades -- Why is it originally inside blades?

bladesSt:OnUpdate(function(value)
	BladesSt.Value = value
end)

local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = coins:Get(0)
Coins.Parent = Fold

coins:OnUpdate(function(value)
	Coins.Value = value
end)

local Rank = Instance.new("StringValue")
Rank.Name = "Class"
Rank.Value = Ranks:Get(DefaultRank)
Rank.Parent = Fold

Ranks:OnUpdate(function(value)
	Rank.Value = value
end)

local ClassVals = Instance.new("IntValue")
ClassVals.Name = "ClassValues"
ClassVals.Value = RebirthValues:Get(DefaultRebirthsValue)
ClassVals.Parent = Rank

RebirthValues:OnUpdate(function(value)
	ClassVals.Value = value
end)

local Full = Instance.new("BoolValue")
Full.Name = "Full"
Full.Value = FullSignal:Get(false)
Full.Parent = Blades

FullSignal:OnUpdate(function(value)
	Full.Value = value
end)
end

for _, player in pairs(game.Players:GetPlayers()) do
playerAdded(player)
end

game.Players.PlayerAdded:Connect(playerAdded)

Tool Script

local RepStorage = game.ReplicatedStorage
local Events = RepStorage.Events – remember to cal event or nil
local CoinClicked = Events.CoinClicked
– Finding The Vals
local Settings = script.Parent.Settings
local Strength = Settings.Strength
local tool = script.Parent
local Debounce = false

tool.Activated:Connect(function()
if player.leaderstats.Blades.Value < player.leaderstats.Blades.BladesSt.Value then
if Debounce == false then
wait(2.5)

		workspace.AddPoints.AddPointsEvent:FireServer()
		
		wait(.2)
		
		local Debounce = true
		
		wait(4.5)
		
		local Debounce = false
		
	elseif player.leaderstats.Blades.Value == player.leaderstats.Blades.BladesSt.Value then
		script.Disabled = true
	else
		script.Disabled = false
	end
end

There u go

2 Likes

nope does not work in real game tho

Now i changed it to a new code does not save again

local DataStore = game:GetService("DataStoreService"):GetDataStore("Blades")
local DataStore1 = game:GetService("DataStoreService"):GetDataStore("BladesSt")
local DataStore2 = game:GetService("DataStoreService"):GetDataStore("Coins")
local DataStore3 = game:GetService("DataStoreService"):GetDataStore("Class")
local DataStore4 = game:GetService("DataStoreService"):GetDataStore("ClassValues")
local DataStore5 = game:GetService("DataStoreService"):GetDataStore("FullSignal")
local DataStore6 = game:GetService("DataStoreService"):GetDataStore("SwordBoost")

local DefaultsBoost = 1
local DEFAULTS= 15
local DefaultRank = "Noob"
local ClassBoost = 1

function PlayerAdded(player)
	
	local playerUserId = "Player_"..player.userId
	
	print(playerUserId)
	
	local Fold = Instance.new("Folder")
	Fold.Name = "leaderstats"
	Fold.Parent = player
	
	local Blades = Instance.new("NumberValue")
	Blades.Name = "Blades"
	Blades.Parent = Fold
	Blades.Value = DataStore:GetAsync(playerUserId) or 0
	
	local BladesSt = Instance.new("NumberValue")
	BladesSt.Name = "BladesSt"
	BladesSt.Parent = Blades
	BladesSt.Value = DataStore1:GetAsync(playerUserId) or 0 and DEFAULTS
	
	local Coins = Instance.new("NumberValue")
	Coins.Name = "Coins"
	Coins.Parent = Fold
	Coins.Value = DataStore2:GetAsync(playerUserId) or 0
	
	local Class = Instance.new("StringValue")
	Class.Name = "Class"
	Class.Parent = Fold
	Class.Value = DataStore3:GetAsync(playerUserId) or 0 and DefaultRank
	
	local ClassValues = Instance.new("NumberValue")
	ClassValues.Name = "ClassValues"
	ClassValues.Parent = Class
	ClassValues.Value = DataStore4:GetAsync(playerUserId) or 0 and ClassBoost
	
	
	local Full = Instance.new("BoolValue")
	Full.Name = "Full"
	Full.Parent = Blades
	Full.Value = DataStore5:GetAsync(playerUserId) or 0
	
	local SwordBoost = Instance.new("NumberValue")
	SwordBoost.Name = "SwordBoost"
	SwordBoost.Parent = Blades
	SwordBoost.Value = DataStore6:GetAsync(playerUserId) or 0 and DefaultsBoost
end

function PlayerRemoving(player)
	local playerUserId = "Player_"..player.userId
	
	DataStore:SetAsync(playerUserId, player.leaderstats.Blades.Value)
	DataStore1:SetAsync(playerUserId, player.leaderstats.Blades.BladesSt.Value)
	DataStore2:SetAsync(playerUserId, player.leaderstats.Coins.Value)
	DataStore3:SetAsync(playerUserId, player.leaderstats.Class.Value)
	DataStore4:SetAsync(playerUserId, player.leaderstats.Class.ClassValues.Value)
	DataStore5:SetAsync(playerUserId, player.leaderstats.Blades.Full.Value)
	DataStore6:SetAsync(playerUserId, player.leaderstats.Blades.SwordBoost.Value)
	
	
	print("Data Saved!")
end

for _, player in pairs(game.Players:GetPlayers()) do
	PlayerAdded(player)
	PlayerRemoving(player)
end



game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoving)

Even the print values from the removing funcs dont print out

@FerbZides

With both of the DataStore scripts that you have provided there could be a couple of things causing your data not to save:

  • You may be hitting your DataStore limits in both your examples. While using DataStore2 you should be combining your data together and while using normal DataStores you should be using one DataStore instead of 7 in your example. You can read more about data store limits here: Documentation - Roblox Creator Hub

  • In your second example(the one above this reply) your saves may be failing. You should put all your DataStore requests in a pcall and consider using UpdateAsync instead of SetAsync. You can read more on the DataStore page on the developers hub because it specifically says to use UpdateAsync instead of SetAsync: Data Stores | Documentation - Roblox Creator Hub


How to use DataStore2
In your DataStore2 example I noticed you never set any data and only update the leaderstats in the player. Updating the leaderstats in the player wont automatically save the data in DataStore2 because you need to call a saving method on the server for it to save.

With DataStore2 you should never save the data when the player leaves the game but instead update the data when the data actually changes. Lets say you have a coins system, you should be saving the players coins whenever they change instead of waiting until the player leaves the game or waiting 6 seconds between each save.

Another thing you should use with DataStore2 is combined data stores. Here is a little article about combined data stores made by Kampfkarren(the creator of the DataStore2 module): Gotchas - DataStore2

Here is a very basic example of how to use DataStore2 using combined data stores:

local DataStore2 = require(script.Parent.DataStore2)

DataStore2.Combine("Data", "Coins") -- Combines the datastores

local function OnPlayerAdded(Player)
	local CoinsStore = DataStore2("Coins", Player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local Coins = Instance.new("NumberValue")
	Coins.Name = "Coins"
	Coins.Value = CoinsStore:Get(0)
	Coins.Parent = leaderstats
	
	CoinsStore:OnUpdate(function(NewValue) -- Fires when the data updates
		Coins.Value = NewValue
	end)
	
	leaderstats.Parent = Player
end

game.Players.PlayerAdded:Connect(OnPlayerAdded)

The below script is a very basic example of how to update the players data, in this case coins, using DataStore2 assuming you are using the OnUpdate function to update the leaderstats. As a word of warning you shouldn’t use this code without the correct server sided checks.

local DataStore2 = require(script.Parent.DataStore2)

game.ReplicatedStorage.Events.UpdateCoins.OnServerEvent:Connect(function(Player)
	-- Before updating data you need to do some checks
	local CoinsStore = DataStore2("Coins", Player)	
	CoinsStore:Increment(1)
end)

i understand but i have to make multiple leaderstats to make my game work for it is there any way to use it with multiple leaderstats

anyways how would i change my class value if its only increment and the class boost and the storage

Even 1 DataStore Cant Even Save

Are you on about the combined data stores? If so here is a little example: Lets say you have multiple leaderstats like: Coins, Points and Level ect all you need to do is this:

DataStore2.Combine("Data", "Points", "Coins", "Level")

You should read more about comined data stores and how they work here because this explains it much better than I could: Gotchas - DataStore2

All combined data stores do is put all your data under one big table to reduce the amount of data store calls made. Using combined data stores don’t effect how you use DataStore2 normally. You still get and set data in the same way.

Remember combined data stores are only a thing with DataStore2.


I am sorry but I am a little confused to what you are asking here so please could you clarify. Please could you clearly explain your use case is and what you are trying to achieve. What is class referring too?

local DataStore2 = require(script.Parent.DataStore2)
local Defaults = 15
local DefaultRank = "Noob"
local DefaultBoost = 1

DataStore2.Combine("Data", "Blades", "BladesSt", "Coins", "RanksVal", "ClassBoostVals")

local function OnPlayerAdds(Player)
	local BladesStore = DataStore2("Blades", Player)
	local BladesStStore = DataStore2("BladesSt", Player)
	local CoinStore = DataStore2("Coins", Player)
	local RankValsStore = DataStore2("RanksVal", Player)
	local ClassBoostStore = DataStore2("ClassBoostVals", Player)
	
	local Fold = Instance.new("Folder")
	Fold.Name = "leaderstats"
	
	local Blades = Instance.new("NumberValue")
	Blades.Name = "Blades"
	Blades.Value = BladesStore:Get(0)
	
	BladesStore:OnUpdate(function(updatedValue)
		Blades.Value = updatedValue
	end)
	
	local BladesSt = Instance.new("NumberValue")
	BladesSt.Name = "BladesSt"
	BladesSt.Value = BladesStStore:Get(Defaults)
	BladesSt.Parent = Blades
	
	BladesStStore:OnUpdate(function(updatedValue)
		BladesSt.Value = updatedValue
	end)
	
	local Coins = Instance.new("NumberValue")
	Coins.Name = "Coins"
	Coins.Value = CoinStore:Get(0)
	
	CoinStore:OnUpdate(function(updatedValue)
		Coins.Value = updatedValue
	end)
	
	local RankClass = Instance.new("StringValue")
	RankClass.Name = "Class"
	RankClass.Value = RankValsStore:Get(DefaultRank)
	
	RankValsStore:OnUpdate(function(updatedValue)
		RankClass.Value = updatedValue
	end)
	
	local ClassBoost = Instance.new("NumberValue")
	ClassBoost.Name = "ClassValues"
	ClassBoost.Parent = RankClass
	
	ClassBoostStore:OnUpdate(function(updatedValue)
		ClassBoost.Value = updatedValue
	end)
	
	Fold.Parent = Player
	
	
end

game.ReplicatedStorage.Events.AddPointsEvent.OnServerEvent:Connect(function(Player)
	local BladesStore = DataStore2("Blades", Player)
	
	BladesStore:Increment(1 * Player.leaderstats.Class.ClassValues.Value)
	
end)

game.ReplicatedStorage.Events.Sell.OnServerEvent:Connect(function(Player, Blades, Coins)
	
	local CoinStore = DataStore2("Coins", Player)
	
	CoinStore:Increment(Coins.Value == Coins.Value + Blades.Value)
	
end)

game.Players.PlayerAdded:Connect(OnPlayerAdds)

A new thing i made

Ignore broken code please :smiley:

Im almost fixing this issue :smiley: :

Are you still having saving issues with this code? This code looks fine to me bar one section:

game.ReplicatedStorage.Events.Sell.OnServerEvent:Connect(function(Player, Blades, Coins)
	local CoinStore = DataStore2("Coins", Player)
	CoinStore:Increment(Coins.Value == Coins.Value + Blades.Value)
end)

In this section you are using Increment slightly wrong. Here is how you should call Increment in the code above:

CoinStore:Increment(Coins.Value + Blades.Value)

Now im confused on sell when removing my blades it should add the to the coins how do i do that