Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Ignoring something in a table?

Asked by 7 years ago
Edited 7 years ago

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

0
As general advice: designs tend to work better when they talk in terms of what you want to INCLUDE rather than what you want to EXCLUDE (because it makes changing design in future easier!) BlueTaslem 18071 — 7y

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
7 years ago

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.

0
next, is better than pairs(). Do speed tests. AlreadyPro 153 — 7y
0
Not if you're iterating over your tables sanely. If you *really* need speed, use a numeric instead of a generic. adark 5487 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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.

Answer this question