Data Not Saving 2

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

If a Single Player leaves i will make sure to save their data!

  1. What is the issue? Include screenshots / videos if possible!

Before Their Data Was Here

After leaving and rejoins the game

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

My solutions that i have done are
1.Server Side Points Are Changing The Values
2.Making a tool that adds points
3.Doing the saving on the game
4.Some Debugging

if u want the source code here it is:

local StickStore = game:GetService("DataStoreService"):GetDataStore("Sticks")
local CoinStore = game:GetService("DataStoreService"):GetDataStore("Coins")
local ClassStore = game:GetService("DataStoreService"):GetDataStore("Class")
local ClassBoostStore = game:GetService("DataStoreService"):GetDataStore("ClassBoost")
local MaxCapStore = game:GetService("DataStoreService"):GetDataStore("MaxCap")
local X2CoinsStore = game:GetService("DataStoreService"):GetDataStore("X2Coins")
local X2SticksStore = game:GetService("DataStoreService"):GetDataStore("X2Sticks")
local plrsLeft = 0
local DefaultBoost = 1
local DefaultRank = "Noob"
local DefaultCap = 15

game.Players.PlayerAdded:Connect(function(player)
	plrsLeft = plrsLeft + 1
	
	
	local plrFold = Instance.new("Folder")
	plrFold.Name = "leaderstats"
	plrFold.Parent = player
	
	local Sticks = Instance.new("NumberValue")
	Sticks.Name = "Sticks"
	Sticks.Parent = plrFold
	Sticks.Value = 0
	
	local Coins = Instance.new("NumberValue")
	Coins.Name = "Coins"
	Coins.Parent = plrFold
	Coins.Value = 0
	
	local Class = Instance.new("StringValue")
	Class.Name = "Class"
	Class.Value = DefaultRank
	Class.Parent = plrFold
	
	local Caps = Instance.new("NumberValue")
	Caps.Parent = Sticks
	Caps.Name = "MaxCapacity"
	Caps.Value = DefaultCap
	
	local BoostRank = Instance.new("NumberValue")
	BoostRank.Name = "ClassBoost"
	BoostRank.Parent = Class
	BoostRank.Value = DefaultBoost
	
	local X2Sticks = Instance.new("NumberValue")
	X2Sticks.Name = "X2Sticks"
	X2Sticks.Value = 1
	X2Sticks.Parent = Sticks
	
	local X2Coins = Instance.new("NumberValue")
	X2Coins.Name = "X2Coins"
	X2Coins.Parent = Coins
	X2Coins.Value = 1
	
	local data
	local data1
	local data2
	local data3
	local data4
	local data5
	local data6
	local data7
	
	local success, errormessage = pcall(function()
		StickStore:GetAsync(player.UserId, Sticks.Value)
		CoinStore:GetAsync(player.UserId, Coins.Value)
		ClassStore:GetAsync(player.UserId, Class.Value)
		ClassBoostStore:GetAsync(player.UserId, BoostRank.Value)
		MaxCapStore:GetAsync(player.UserId, Caps.Value)
		X2CoinsStore:GetAsync(player.UserId, X2Coins)
		X2SticksStore:GetAsync(player.UserId, X2Sticks)
	end)
	
	if success then
		local data = Sticks.Value
		local data1 = Coins.Value
		local data2 = Class.Value and DefaultRank
		local data3 = BoostRank.Value and DefaultBoost
		local data4 = Caps.Value and DefaultCap
		local data5 = X2Coins.Value
		local data6 = X2Sticks.Value
	else
		warn("The Data Cannot Be Loaded!")
	end
	
end)

local BindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
	
	
	
	plrsLeft = plrsLeft - 1
	
	local data = player.leaderstats.Sticks.Value
	local data1 = player.leaderstats.Coins.Value
	local data2 = player.leaderstats.Class.Value
	local data3 = player.leaderstats.Class.ClassBoost.Value
	local data4 = player.leaderstats.Sticks.MaxCapacity.Value
	local data5 = player.leaderstats.Coins.X2Coins.Value
	local data6 = player.leaderstats.Sticks.X2Sticks.Value
	
	
	
	local success, errormessage = pcall(function()
		StickStore:GetAsync(player.UserId, data)
		CoinStore:GetAsync(player.UserId, data1)
		ClassStore:GetAsync(player.UserId, data2)
		ClassBoostStore:GetAsync(player.UserId, data3)
		MaxCapStore:GetAsync(player.UserId, data4)
		X2CoinsStore:GetAsync(player.UserId, data5)
		X2SticksStore:GetAsync(player.UserId, data6)
		
	end)
	
	if success then
		warn("The Players Data Was Saved!")
	else
		warn("There was an error while saving the players Data!")
	end
	
	BindableEvent:Fire()
end)

game:BindToClose(function()
	while plrsLeft > 0 do
		BindableEvent:Wait()
	end
end)

You may think i just want to ask for some codes but im technically looking for a fix on this

Your Solutions So Far Like Last Time:
1.Switch To DataStore2 (Im not doing this)
2.Print And Debugging (Cleared)

Thanks
~FerbiZides

you really should be using one key to store all of the players data

2 Likes

firstly that, and secondly (at least in my case) data doesnt even save in studio anymore, despite the api being physically able to

1 Like

Even In The Real Game Does Not Save Too What Is Wrong with my code it seems to be fine

I strongly suggest that you use DataStore2. It uses caching and advanced saving methods to prevent data loss. If you want to save in studio, DataStore2 also supports that.

DataStore2 also combines datastores to prevent throttling, which would be ideal in your case.

DataStore2 which i have been using along time but its more neat to be using normal DataStores

I disagree.

The current setup you are using is definetly going to throttle.
You are making multiple calls on both PlayerAdded and PlayerRemoving. This will definetly surpass the throttle limit and will eventually lead to data not being saved or being saved late.

DataStore2 combines all of them into one call which wil probably never throttle.

P.S: Please try to use proper english, I’m having some difficulties understanding.

I am seeing lots of people recommend DataStore2 and I agree with them. If you want to have a reliable data store without the complexity of them you should use DataStore2. However just suggesting to use DataStore2 isn’t helping much with learning how to use normal data stores.

Avoid using multiple data stores

You shouldn’t have a data store for each thing you want to save in your game because you will end up hitting your limits very quickly. You should try limiting it to 2 at most and in most cases using one is better. To limit the amount of data store calls you do you should save everything under one big table and create a cache of your data in your game. Here is a little example:

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

local Cache = {
	[123456789] = { -- Players UserId
		Coins = 20;
		Level = 30;
		Points = 100;
	}
}

game.Players.PlayerRemoving:Connect(function(Player)
	local Success, Error = pcall(function()
		DataStore:UpdateAsync(Player.UserId, function(OldData)
			return Cache[Player.UserId]
		end)
	end)
end)

Having a cache will avoid you making anymore GetAsync request other than from when the player joins the game. You will also be able to save the data directly from the cache and do all your validations from it.

Incorrect use of pcall

The reason why your data store isn’t saving or loading is because you are using pcall wrong. You are doing all your data store requests in one pcall meaning that if one fails you are going to assume all of them have . As you are doing 7 data store calls in your pcall it is going to always fail because once you are on your 7th request you would have already hit your limits. To fix this you should always only ever have one request in each pcall you do. If your data is saved under one big table you will only ever need to do one data store request when the game starts up.

For a better usage of pcall you shouldn’t create a variable outside the pcall to assign the data too. Instead you should do something like this:

local Success, Result = pcall(function()
	return DataStore:GetAsync(tostring(Player.UserId))
end)

I can’t see you using any saving methods

From the code that you have provided I can’t see any saving methods because in your PlayerRemoving function you are only getting the players data. The recommend way of saving your data is through UpdateAsync. I won’t be explaining how to specifically do this because there is a great tuturial about using UpdateAsync to save data:

For using BindToClose you may want to look at this post because it example a good method of how to use it:

6 Likes

Basically, What I Need To Do Is Store Them In Tables Then Use UpdateAsync() rather than SetAsync
well if we look here the Cache need to be player.userId.

Then
We Will Save The Values In A Single Table Using DataStore.

I will use DataStore2 if I can but in the mean time you count this as solved

Tables are efficient in every way SetAsync()
Was not good Well Thanks.