This is getting all the values inside of a folder however it errors because there's another folder inside of it called "PlayerAbilities" How would I make it ignore PlayerAbilities when it does "GetChildren()"
SaveStats = function(Player) local UserId = Player.UserId PlayerStats[UserId] = {} for i,v in next, GlobalStats[UserId]:GetChildren() do PlayerStats[UserId][v.Name] = v.Value end if VerifySavedData then print(Player,UserId) for i,v in next, PlayerStats[UserId] do print(i,v) end end end
I really wish I could smack whoever spread that using next
is better than using pairs
. It's really not, it just hampers readability.
Anyway, what you want to do is use an if
statement to only include what you want:
SaveStats = function(Player) local UserId = Player.UserId PlayerStats[UserId] = {} for _,v in pairs(GlobalStats[UserId]:GetChildren()) do if v.Name ~= "PlayerAbilities" then PlayerStats[UserId][v.Name] = v.Value end end if VerifySavedData then print(Player,UserId) for i,v in pairs(PlayerStats[UserId] do print(i,v)) end end end
In this case, I would suggest putting the Value Objects you wish to save inside a Folder so that you don't have to exclude certain things. That way, the code will still work if you add another Folder that you also have to exclude, for instance.
There is no way to add an ignore list to GetChildren()
we will have to make an ignore list to exclude this folder as we get the data back in a random order.
Example:-
-- the ignore list local ignore = { ['a'] = true, ['b'] = true } local test = { 'hallo', 'a', 'testing', 'b', 'test end' } for i,v in pairs(test) do if ignore[v] then -- bad items else print(v) -- good item end end
I hope this helps, please comment if you do not understand how / why this code works.