Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Ignoring something in a table?

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

1 comment

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!)

2 answers

✓ Accepted answer

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.

2 comments

0

next, is better than pairs(). Do speed tests.

0

Not if you're iterating over your tables sanely. If you really need speed, use a numeric instead of a generic.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.