Well, try the other advise i gave you.
I already said this is in ServerScriptService.
Iâm referring to this, not the one about the serverscript location and itâs type.
Your not making any sense, do you mean make a starting value of the cash and give it to whoever is new to the game?
Erase that, replace the setAsync function inside the playerRemoving with this.
local success, err = pcall(function()
ds:SetAsync(id, plr.leaderstats.Cash.Value)
end)
Replace the one inside the playerAdded with this.
local success, errormessage = pcall(function()
data = ds:GetAsync(id)
end)
if success then
print("Successfully obtain data!")
cash.Value = data
else
warn("Failed to load data.")
end
Just try it, itâs not like iâm in studio and iâm like âmhm, thisâll 100% work!â
No, I am gonna store multiple datas with it, so I am creating a table with it.
Zeus, you could make different equivalents of the data variable in the playerAdded and in the playerRemoving functions.
I didnât create any equivalents with it. What are you even talking about?
This is for an example.
local id = plr.UserId.."-cash"
local id2 = plr.UserId.."-victories"
local data
local data2
local success, errormessage = pcall(function()
data = ds:GetAsync(id, data)
data2 = ds:GetAsync(id2, data2)
end)
Iâm not 100% sure if itâs correct.
Edit: I donât have the programmer role for a reason.
Sorry for the delayed response, I was pretty busy and now I tested and this seems to work alright. I also added a few lines / changed, for simplicity.
local DSS = game:GetService("DataStoreService")
local ds = DSS:GetDataStore("CashAndWins")
local run = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder",plr)
folder.Name = "leaderstats"
local cash = Instance.new("IntValue",folder)
cash.Name = "Cash"
local id = plr.UserId.."_Data"
local data
local success, errormessage = pcall(function()
data = ds:GetAsync(id)
end)
if success then
print("Successfully obtain data!")
cash.Value = data.Cash or 0
else
warn("Failed to load data.")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local id = plr.UserId.."_Data"
local data = {
Cash = plr.leaderstats.Cash.Value
}
local success, err = pcall(function()
ds:SetAsync(id, data)
end)
if success then
print("Saved")
else
print("Data hasn't been saved.")
warn(err)
end
end)
Some things you should know:
- Client / Local scripts canât have access to Data Store Service.
- It is much better to store the player data in one key, imo
I am sorry but, it is still a fail. No errors are found.
It didnât seem to print the print Saved
while I was testing, so I switched to server side and then removed the player from the game and seems like it is working and printing Saved. Not really sure why this is the behavior though.
And I am sure it is saving because I had changed the leaderstat value from the server side and on rejoining it works as expected.
Well, Iâve tested in my game instead studio. Guess what?
IT IS FINALLY WORKING. THANK YOU SO MUCH.
I will analyze the script carefully and try to recreate it without looking it, because of you helping me a very hard and long journey, you earned my respect of following you.
Once again, thank you!
Also, i think the issue is because your datastore saves atleast every 30 seconds, zeus was pretty fast to setting his value and trying to exit the game then join back in to see if it saved, anyways you should create a coroutine thread and do something with it and whatever then itâll repeat it. It explains a lot in this topic i sent earlier, you should read more.
Edit: nvm
This isnât the error, the script doesât know what value to set to.
Ho sorry, I explained myself poorly, use both