local DataStoreService = game:GetService("DataStoreService") local dataStore = DataStoreService:GetDataStore("dataStore") 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 Wins = Instance.new("IntValue") Wins.Name = "Wins" Wins.Parent = leaderstats local playerUserId = "Player_" .. player.UserId --Load data local data local success, errormessage = pcall(function() data = dataStore:GetAsync(playerUserId) end) if success then print("Data successfully loaded!") Cash.Value = data.Cash Wins.Value = data.Wins else print("There was an error loading data!") warn(errormessage) end end) game.Players.PlayerRemoving:Connect(function(player) local playerUserId = "Player_" .. player.UserId local data = { Cash = player.leaderstats.Cash.Value; Wins = player.leaderstats.Wins.Value; } local success, errormessage = pcall(function() dataStore:SetAsync(playerUserId, data) end) if success then print("Data successfully saved!") else print("There was an error saving data!") warn(errormessage) end end)
How do I fix this? The error is attempting to index nil with cash. I am trying to save the table to the datastore but do not know how to fix this error. I know where the error is and why there is an error, but what am I meant to do about it.
The Problom Here is that , your script nerver Creates any data at all , Its just trying to get data.
And the data it is trying to get has never benn created so lets create it like this... :
local DataStoreService = game:GetService("DataStoreService") local dataStore = DataStoreService:GetDataStore("dataStore") local STARTER_CASH = 0--- Put Your Starter Amount Of Cash Here ! local STARTER_WINS = 0--- Put Your Starter Amount Of Cash Here ! 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 Wins = Instance.new("IntValue") Wins.Name = "Wins" Wins.Parent = leaderstats local playerUserId = "Player_" .. player.UserId --Load data local data local success, errormessage = pcall(function() data = dataStore:GetAsync(playerUserId) if not data then ----- If The Player Does not Have Any Data , then lets Create New Data for them ! local NewData = { Cash = STARTER_CASH, Wins = STARTER_WINS} local Made , errormsg = pcall(function() dataStore:SetAsync(playerUserId, NewData) end) if Made then print(" Created New Data For ".. player.Name .." ! :D ") else warn("Could Not Create Data For ".. player.Name.." ! D: Error Message : ".. errormsg) end end end) if success then print("Data successfully loaded!") Cash.Value = data.Cash Wins.Value = data.Wins else print("There was an error loading data!") warn(errormessage) end end)
And That Is How you create New Data For New Players ! :D
I also Added New Variables Called STARTER_CASH
and STARTER_WINS
so you can customised the amount of cash and wins a new player gets !
Good Day !
Issue solved. Just had to add an if statement to check whether data was nil, then if it was I just set the cash and wins to 0.