I am making a save script that saves a list of booleans. I am trying to save whether someone has a perk or not, and when I try and load the saves it says
"ServerScriptService.SavePerks:21: attempt to index nil with number". Below is the script I'm working with. -Solved
For some reason, the script isn't changing the player's values. I don't see any errors, but I'm new to scripting so maybe one of you guys might. Also, when I leave the game, it says there is an error while trying to save the data. There is no error code or message it just warns that "There was an error while loading data! Errormessage:".
local DataStoreService = game:GetService("DataStoreService")
local List = DataStoreService:GetDataStore("List")
local PerkList = {"CammoBottle", "Crocodile", "Poo"}
local PerkStatus = {true, true, false}
local Temp = {}
-- true means it is defaulted to on. false means it is defaulted to off.
-- Values will change depending on what a person has saved
game.Players.PlayerAdded:Connect(function(plr)
local Data
local success, errormessage = pcall(function()
Data = List:GetAsync(plr.UserId.."-List2")
end)
if success then
wait(0.1)
for _, e in pairs(plr.Values.Perks:GetChildren()) do
for i, e2 in ipairs(PerkList) do
print(e.Name..", "..e2)
if e.Name == e2 then
e.Value = Data[i]
print(e.Name..", "..Data[i])
end
end
end
print("Perk Data Loaded")
else
warn("There was an error while loading data! Errormessage:")
warn(errormessage)
warn("Defaulting Perks")
for _, e in pairs(plr.Values.Perks:GetChildren()) do
for i, e2 in ipairs(PerkList) do
if e.Name == e2 then
e.Value = PerkStatus[i]
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
for _, e in pairs(plr.Values.Perks:GetChildren()) do
for i, e2 in ipairs(PerkList) do
if e.Name == e2 then
table.insert(Temp, i, e2[i])
end
end
end
List:UdateAsync(plr.UserId.."-List2", Temp)
end)
if success then
print("Data Saved")
else
warn("There was an error when saving data! Errormessage:")
wait(errormessage)
end
end)
This is what the game.Players[player].Values folder is: Click to view
This is not extremely important but if I want to add more perks to my game this will make it easier.

5 comments
Data is nil. The reason behind this is because a DataStore entry hasn't been established yet for the newcomer. You should accommodate for this by adding an additional clause of "and Data ~= nil".
You should also use a Dictionary in the format of: "{ ["Perk Name"] = {PerkStatus = --[[Boolean]]}; }". This will merge both PerkList & PerkStatus, while brining general ease and efficiency.
If you don't understand the difference between "pairs" and "ipairs", don't switch between them.
I would assume that "pairs" is random and "ipairs" lists them in order. Im not sure im sortof new to scripting. I searched up if I could save a dictionary on roblox and it said I cant, that is why Im using tables. And I have a script that is supposed to save the default perk settings when you leave.. oh wait, i just realized that when I was leaving the test I was pressing stop instead of kicking.
Ok, I added the line of code to line 20 and there is no longer an error, but the player values aren't being set to true. Any idea why?