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.