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.

2 comments
What Line is the Error on ?
nvm , I think I see the problem , I will go and answer it !