Datastore Script Help

Scripting help:

  1. What do you want to achieve?
    I want to datastore an Integer Value

  2. What is the issue? !
    The code doesnt work/stores the value.

  3. What solutions have you tried so far? ?
    Tried looking everywhere still no hope

CODE:

local datastore = game:GetService("DataStoreService")

local ds1 = datastore:GetDataStore("GemSaveSystem")
local ds2 = datastore:GetOrderedDataStore("TimeWasted")

game.Players.PlayerAdded:Connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 
-- local gems = Instance.new("IntValue", folder)
-- gems.Name = "Gems"

 local cash = Instance.new("IntValue", folder)
 cash.Name = "TimeWasted"
 
-- gems.Value = ds1:GetAsync(plr.UserId) or 0
-- ds1:SetAsync(plr.UserId, gems.Value)
 
 cash.Value = ds2:GetAsync(plr.UserId) or 0
 ds2:SetAsync(plr.UserId, cash.Value)
 
-- gems.Changed:connect(function()
--  ds1:SetAsync(plr.UserId, gems.Value)
-- end)
 
 cash.Changed:Connect(function()
  print(" Data changed")
  ds2:SetAsync(plr.UserId, cash.Value)
 end)
end)

Please can someone explain me what is wrong over here?
I just dont understand why it isnt saving the value in Datastore.

It is also suppose to print “Data changed”
when it updates the value.

Searching is not the only thing you should do. Take time to review your code, debug and confer with existing resources.

Off the bat I see three issues: use of the parent argument of Instance.new, using SetAsync every time cash changes (leading to quickly exhausted budget and throttled requests) and lack of any pcall on your methods (if DataStores are down, players who encounter inability to load their stores will get their data overwritten by a default).

As for how this isn’t working, no such explanation is visible. How is it not working?

Whenever the value changes it is suppose to overwrite that value in the datastore.
It is not printing “Data changed”

So if it isn’t printing “data changed” that means the code below is not being executed and
well the data is not saved when I leave the game

This sounds like a case of lack of application of basic debugging. There’s nothing in that code that doesn’t work but it employs bad practice and should not be used in a production environment.

Where are you testing coin changes from? Do you start a Studio session and then modify the value? This is registered as a client-side change and will not be seen by the server, thus not changing the value. If Studio access to API services isn’t on either, you cannot use DataStores in Studio.

Look through your console for errors.

Yes I have made a while loop that adds +1 value to cash every second

@PostApproval the problem is there are not any errors.

And yes it is not for an production environment, I am just testing and learning how to use datastores

Yes its a local script
30chars

See:

You need to apply basic debugging to your code or actually look at what you’re doing. You aren’t getting any prints because the value isn’t actually being changed server-side. LocalScript changes to values do not replicate.

You mean this datastore script or the script that adds a value to cash every second?

Both.

i never have much luck with the value.Changed event tbh. I prefer to use other methods when working with this kinda situation (ofc, you should always remember to replicate your changes to the server with a remote event or something)

Oh okay I will test it by changing it to server script

edit- I will do it tomorrow its 2 am and I need to sleep