Finding stuff in table

-- // Random 
local CorrespondingReasons = {
	[1] = "Detected HopperBins",
	[3] = "Detected Character modifications", 
	[4] = "Detected Connections disconnected",
	[5] = "Detected Workspace modifications",
	
	[0] = "Disconnected" -- // Random disconnect reason...
	-- adding more
}

How can I index this for a certain number like index for 5 and return "Detected Workspace modifications", and if what im trying to index does not exist than return

1 Like
-- // Random 
local CorrespondingReasons = {
	[1] = "Detected HopperBins",
	[3] = "Detected Character modifications", 
	[4] = "Detected Connections disconnected",
	[5] = "Detected Workspace modifications",
	
	[0] = "Disconnected" -- // Random disconnect reason...
	-- adding more
}

local function getReason(n: number)
    return CorrespondingReasons[n or 1] or ""
end
1 Like

Wait so if the key does not exist it returns a empty string?

Yes, if no value was found in the table it will return an empty string and if no number was passed, it will return “Detected HopperBins” (index 1).

1 Like

Thanks bro
#CCCCCCCCCCCCCC<><><>

1 Like

This seems to return none when indexing number 2,
number 1 returns as expected.

because there isn’t a second index in the table. You only have 0, 1, 3, 4 and 5. Maybe you removed 2 by mistake.

1 Like

Thats my bad, my co-developer removed the reason.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.