It is because earlier, your data structure was pretty different and it probably had saved data in that structure itself. It should work alright if you change the key for the DataStore.
local success, errormessage = pcall(function()
data = ds:GetAsync(id)
end)
Where is the second variant? If you added 2 variants in the playerRemoving function then you need to add the second one too.
Edit: In the error output, it said it attempted to index number with âCashâ, the number it was referring to is the playerâs userId, and the âCashâ is the missing variant it couldnât find.
So I just change the name of the key to something else like Cash2?
What do you mean by this? Please brighten me up more.
You need to add 1 more variant instead of just âidâ.
local success, errormessage = pcall(function()
data = ds:GetAsync(id, "missing variant")
end)
What do you mean by missing variant?
No, I mean like the Datastore key that you use for referencing the Record of the player. Here is the edited script:
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 key = plr.UserId.."_Data"
local data
local success, errormessage = pcall(function()
data = ds:GetAsync(key)
end)
if success then
print("Successfully obtain data!")
cash.Value = data.Cash
else
warn("Failed to load ata.")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local key = plr.UserId.."_Data"
local data = {
Cash = plr.leaderstas.Cash.Value;
}
local sucess, err = pcall(function()
ds:SetAsync(key, data)
end)
end)
Also there was a small error in the SetAsync, where you were passing the player object as the key, which wasnât the same with what you were referencing in the GetAsync earlier.
Iâm talking about arguments, hereâs an example of a variant.
function die(variant, variant2)
if variant then
variant2.Health = 0
end
end)
die(game.Workspace.reygenne1, game.Workspace.reygenne1.Humanoid)
I donât think you are making any sense, GetAsync accepts only the key you want to get the data for.
ok iâll do some research my bad
So is this script fully correct now?
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 = "Cash2"
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.Cash2
else
warn("Failed to load data.")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local id = plr.UserId.."_Data"
local data = {
Cash2 = plr.leaderstats.Cash2.Value;
}
local sucess, err = pcall(function()
ds:SetAsync(id,data)
end)
if success then
print("data saved")
end
end)
Also why did you get the RunService?
Try it, who knows it might work or not.
You donât need to rename the cash stat to Cash2 though, it should work.
Although there is a typo mistake in your script in the end, where you referenced success but you named the variable sucess
Also, thereâs still gonna problems even if the script is perfectly fine, the server the user is in may close for some reasons and it wonât count as a playerRemoving, you could use âBindToCloseâ where if the game closes, it will fire a function thatâll save some userâs data and their stats. Although iâm a bit lazy this topic could help you with that.
Nope, it still fails. I didnât print out data saved.
Any errors in the console or anything else we can use to trace it?
It doesnât show an error. All it loads is data loaded successfully.
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
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("data saved")
else
print("failed")
end
end)
You can simply just write the amount of gold that the player had instead of making a index table of a cash that saved the playerâs values, i donât even know how to fix your problem.
Just realized, is that even a normal script or is it localscript? Place that script inside the serverscriptservice for a bit of safety.
This is a server script that is under ServerScriptService, duh.