Still have questions? Join our Discord server and get real time help.
0

# [ISSUE SOLVED] Saving a table to a data store. How do I fix this error?

Asked by 3 days ago
Edited 3 days ago
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("dataStore")

local leaderstats = Instance.new("Folder")

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"

local Wins = Instance.new("IntValue")
Wins.Name = "Wins"

local playerUserId = "Player_" .. player.UserId

local data

local success, errormessage = pcall(function()
data = dataStore:GetAsync(playerUserId)
end)

if success then
Cash.Value = data.Cash
Wins.Value = data.Wins
else
warn(errormessage)
end
end)

game.Players.PlayerRemoving:Connect(function(player)

local playerUserId = "Player_" .. player.UserId

local data = {
}

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.

0
What Line is the Error on ? Tizzel40 225 — 3d
0
nvm , I think I see the problem , I will go and answer it ! Tizzel40 225 — 3d

0
Tizzel40 225
3 days ago
Edited 3 days ago

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 !

local leaderstats = Instance.new("Folder")

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"

local Wins = Instance.new("IntValue")
Wins.Name = "Wins"

local playerUserId = "Player_" .. player.UserId

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)

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
Cash.Value = data.Cash
Wins.Value = data.Wins
else
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 !

1
Thank you for this 800ChickenWings 3 — 3d
0
No problem ! :) ! Tizzel40 225 — 3d