My leaderstats not saving

Is there a function to reset the value of the lb @waterrunner In DataStore2

my lb not changing when using datastore2 here is my sell when the part is touched the server would fire

local RepStorage = game.ReplicatedStorage
local Evs = RepStorage.Events.Sell
local player = game.Players.LocalPlayer -- if this is useful

script.Parent.Touched:Connect(function()
	Evs:FireServer()
end)

With your current set up you are using the OnUpdate function in your main leaderstats script. Whenever increment is called the OnUpdate function is then fired and updates your leaderstats directly with the new value. This means you can’t and shouldn’t directly update the leaderstats value in increment.

Instead you should use just increment a number. Here is a little example of how you can deduct and add values:

CoinsStore:Increment(BladesValue) -- Adds the blades value to the coins store
BladesStore:Increment(-BladesValue) -- removes the blades value from the blades store

For reference here is all the DataStore2 API: API - DataStore2

Just call Set(nil) as this will reset the data.

now i only need to figure out the fire server

use Blades.Value am i right man

fixed it thanks for your help @waterrunner ill try to figure out the others

Don’t increment like that. You’re adding you’re attempting to add a boolean to a numerical value.

To explain let me break it down.

Coins.Value == Coins.Value

That returns a boolean, whenever you compare using >, ==, <, etc you get a boolean value. That’s why if statements function that way.

+ Blades.Value

Doing this is like adding a string to a CFrame, it just won’t work.

I mentioned earlier to set using datastore2, you do that because it automatically updates the datastore and the values itself. NEVER SET THE VALUE OBJECTS

If you’re selling something, you should do this:

...
local Blades = DataStore2("Blades", player)
local CoinsStore = DataStore2("Coins", player)
if Blades:Get(0) then
	 CoinsStore:Increment(Blades:Get(0)) -- The amount of blades you have collected.  0 is the default value.
	 Blades:Set(0) -- Set the value back to zero because you have sold all of your blades.
end

So Its like Adding the adding DataStores on When i touch the part this runs?

if its 0 then the coins value would be 0 so how do you get the a value from a DataStore all of them are NumberValues

Yes.

I need 30 characters in order to post this.

The :Get(0) Method

Coins:Get(0) -- 0 is the first argument

The 1st argument is the DEFAULT VALUE.

When a new player joins, they have that Default Value. They have 0 coins when they start, however if they player has a DataStore, then their coins equal the amount that is saved.

The coins ARE NOT set to 0

To make this more clear, I will show you what this would look like.

-- New player with no save
Coins:Get(0) --> 0

-- Old player who already has a save
Coins:Get(0) --> 1000

I made a flowchart to help you understand.

Flowchart

save

I want you to get the best understanding possible, and I’m sorry If any of my previous posts weren’t clear enough.

Extra Details

I should also mention that there’s an event called :OnUpdate that I used in my code. It updates when the DataStore is updated. That exists just so when you update DataStore2, you update the values.

when you do :Set(), it updates the numbervalues. It’s a really nice feature of DataStore2, and it saves when the player leaves. (Or when the server closes)

The Takeaway

What you should get from this, is that use datastore2 to update values. You’re updating the NumberValues, which is not updating the datastores themselves.

-- BAD (This is why it won't save!)
Coins.Value = Coins.Value + 10

-- GOOD
local Coins = DataStore2('Coins', player) -- Get the DataStore
Coins:Increment(10) -- INCREMENT ADDS DATA

To continue on what I was going on about, this code sells your blades.

Assuming that you’re collecting blades, this code does this:

  • Checks to see if you have Blades
  • Adds coins based on how many blades you collected
  • And then sets your coins back to zero. (You just exchanged your blades for money!)

Do i just have to do an code like this

game.Workspace.Union:Connect(function()
          -- my code
end)

Should I add that on the union part or the leader stats part :smiley:

No, that’s not what I’m saying. When you set leaderstats it won’t save, use DataStore2:Set(number) and DataStore2:Increment(number) to update stats.

I think that’s what I’m using but it does not work for me or it has something to do with it

Do you mean .Touched? Yes, that will work. AS long as your firing the remote from a LocalScript and hooking the event from the client.

I fired it on the server from a local script

The server wont fire it does not work

If you have multiple datastore keys you can make a table/dictionary and loop through them, thats what I do.

First you need to enable API services.
If you did that you need to insert this script “ServerScriptService”:

local DSService = game:GetService('DataStoreService'):GetDataStore('noob123') -- Don't change noob123

game.Players.PlayerAdded:connect(function(plr)
– Define variables
local uniquekey = ‘id-’…plr.userId
local leaderstats = Instance.new(‘IntValue’, plr)
local savevalue = Instance.new(‘IntValue’)
leaderstats.Name = ‘leaderstats’
savevalue.Parent = leaderstats
savevalue.Name = ‘Cash’ – You can change here

-- GetAsync
local GetSaved = DSService:GetAsync(uniquekey)
if GetSaved then
	savevalue.Value = GetSaved[1]
else
	local NumbersForSaving = {savevalue.Value}
	DSService:SetAsync(uniquekey, NumbersForSaving)
end

end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = ‘id-’…plr.userId
local Savetable = {plr.leaderstats.Cash.Value}
DSService:SetAsync(uniquekey, Savetable)

end)

I hope you enjoy, if I write wrong. Please explain more details.