Hello there!
I’m trying to find an object (player name and id) inside a table with multiple dictionaries inside of it. The object is supposed to be inside one of said dictionaries.
To do so, I used a recursive\deep search function I found here on the DevForum, but unfortunately it keeps returning false, as if the object i’m looking for wasn’t inside the table I pass.
This is the resursive\deep search function in question:
local function deepSearch(t, key_to_find)
for key, value in pairs(t) do
if value == key_to_find then
return true,t
end
if typeof(value) == "table" then
return deepSearch(value, key_to_find),nil
end
end
return false,nil
end
This is the function where I run the aforementioned function and insert the player name and user id. Basically, i use the deepSearch() function to check if the player is already inside one of the dictionaries.
Corrosion.Stages = {{},{},{},{}} --table with dictionaries where i story the players
function Corrosion:triggerStage(plr:Player,corrosion:number,oldCorrosion)
local stage = self:getStage(plr,corrosion)
if stage >= 1 and stage <= 4 then
local isPresent,tb = deepSearch(self.Stages,plr.UserId)
if not isPresent then
self.Stages[stage][plr.Name] = plr.UserId
local corrosionStage = require(script:FindFirstChild(tostring(stage)))
corrosionStage:Start(plr)
warn("just logged")
elseif isPresent then
warn("player found inside one of dictionaries")
local oldStage = self:getStage(plr,oldCorrosion) print(stage,oldStage)
if stage ~= oldStage then
self.Stages[oldStage][plr.Name] = nil
self.Stages[stage][plr.Name] = plr.UserId
local stageOld = require(script:FindFirstChild(oldStage))
local stageNew = require(script:FindFirstChild(stage))
stageOld:End(plr)
stageNew:Start(plr)
print("is this even working?")
end
end
elseif stage == 0 then
warn("not enough corrosion!!")
local isPresent,tb = deepSearch(self.Stages,plr.UserId)
if isPresent then
tb[plr.Name] = nil
local oldStage = self:getStage(plr,oldCorrosion)
require(script:FindFirstChild(tostring(oldStage))):End(plr)
else
warn("i just logged sis")
end
end
print(self.Stages)
end
Does anyone know if I’m doing anything wrong? Perhaps I’m indexing the player inside one of the dictionaries the wrong way, hence why it returns false?
EDIT: this is where I got the deepSearch() function