Data Store problem

Hey,
I’m new in data store so it’s really hard for me. I’m trying to make a data store with 3 values, Win/Cash/Medicine. So for doing this I saw a tutorial of Alvin Blox and copy exactly what is on the video but the script doesn’t save the cash (every print work ok but the print(“Data Saved”) work 1/2). Any error in output. Of course, I have activate data store on Roblox Studio. So what is wrong with this script. Oh and by the way, I have to make this 3 times to save each value separately or use table ?
Thanks for answer :smiley:

 local DataStoreService = game:GetService("DataStoreService")
 local myDataStore = DataStoreService:GetDataStore("myDataStore")

 game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats

local data 
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId.."-cash") or 0
	print("Data load")
end)
if success then
	cash.Value = data
else
	print("Error while loading data")
	warn(errormessage)
end

end)

 game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
	myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
end)
if success then
	print("Data saved")
else	
print("Error while saving data")
warn(errormessage)
end
end)
1 Like

You can save a table in DataStores.

DataStore:SetAsync(PlayerId, {Cash = 100, Wins = 5, Medicine = 32}

Thanks but what is wrong with this script ?
Really thanks for the answer :smiley:

Can you send screenshots of output apon leave and join please, also remember a good thing about PCall is you can encorperate it into a loop to try several times to save the data if it fails

Sorry but what is apon ? And where I have to put this loop on the script.
For Output, it look like that:
[12:53:00.169 - InsertService cannot be used to load assets from the client]
12:53:00.170 - Stack Begin
[12:53:00.171 - Script ‘Plugin_1292342678.PathfindingMaker.ButtonScript’, Line 13]
12:53:00.171 - Stack End
[Data load]
12:53:13.208 - Disconnect from 127.0.0.1|54911

Quick question to help you further in scripting, do you understand some of this Lua scripting, you really should not be dong this if you don’t and you should not just copy a script from someone unless you truly understand it.

1.) it needs to be in a (Server)Script
2.) Having this script written 3 times can lead to a lot of errors so do use tables like @anon92559147 said

Gonna reinforce this point, before you move onto datstoree first understand tables and how values work and can be manipulated, also do a bit of research into the data store api, find out what GetAsync SetAsync UpdateAsync do.

I suggest starting with any basic videos like loops, if’s and such as another reinforcement

Well, my method of saving leaderstats is that I for loop through the :GetChildren() of leaderstats and create it into a dictionary.
Example:

local tblToSave

for _,v in pairs(player.leaderstats:GetChildren()) do
    tblToSave[v.Name] = v.Value
end

Then I save that table.
Once I need to retrieve the data I can simply:

local data

local success, errormsg = pcall(function()
    data = datastore:GetAsync(player.UserId)
end)

if success then
    cash.Value = data.Cash or 0
end