I need help debugging if statements that require detectng a table's type and the string idenifier

Module Code Snippet
if typeof(SpecialEffects) == "table" and table.find(SpecialEffects,AllowedSpecialEffectsTable[6]) then
			for Index, value in pairs(SpecialEffects) do
				if typeof(value) == "table" and Index == "Instances" then
					local NewTable = table.find(SpecialEffects,"Instances")
					local Result = table.find(NewTable,parent)
					if Result then
						NewDamage = Damage * Result
						HandleDamage()
					else
						HandleDamage()
					end
				else
					HandleDamage()
				end
			end
		elseif typeof(SpecialEffects) == "table" and table.find(SpecialEffects,AllowedSpecialEffectsTable[7]) then
			print("Passed 2")
			local Humanoid = parent:FindFirstChildOfClass("Humanoid")
			if Humanoid then
				print("0.5")
				if Damage == nil then --% Damage Only
					local Result1 = table.find(SpecialEffects,"DivideMaxHealth")
					print("1")
					if Result1 == nil then print(SpecialEffects) return end
					print("2")
					local Result2 = table.find(SpecialEffects,"MaxHealthDamage")
					print("3")
					local MoreDamage = nil
					NewDamage = Humanoid.MaxHealth / Result1
					print("5")
					if Result2 then
						MoreDamage = NewDamage / Result2
					else
						MoreDamage = NewDamage
					end
					print("6")
					HandleDamage(true, MoreDamage, Humanoid)
				elseif Damage ~= nil then --% Damage + Original Damage
					local Result1 = table.find(SpecialEffects,"DivideMaxHealth")
					print("1")
					if Result1 == nil then print(SpecialEffects) return end
					print("2")
					local Result2 = table.find(SpecialEffects,"MaxHealthDamage")
					print("3")
					local MoreDamage = nil
					if Result2 then
						MoreDamage = (NewDamage / Result2) + Damage
					else
						MoreDamage = NewDamage + Damage
					end
					print("4")
					HandleDamage(true, NewDamage, Humanoid)
				end
			end
		elseif typeof(SpecialEffects) ~= "table" and Damage ~= nil then
			HandleDamage()
		else
			warn(SpecialEffects)
		end

Before I start talking about my problems, I will go into detail about the most important variables to keep track of.

SpecialEffects is a function parameter. It is the table I am trying to find certain values inside.

AllowedSpecialEffectsTable is a already built in ModuleScript table used to idenify if the SpecialEffects parameter table contains one of the strings in the AllowedSpecialEffectsTable.

Now with that out of the way, here’s my problem. When I try checking the type of the SpecialEffects parameter, it returns “never”. This is something I have yet to be able to figure out and I believe I wrote all my if statements conditionals correctly when type-checking the table. I do not know how typeof(SpecialEffects) == "table" is showing up as a “never” value and it seems odd considering the fact the first if statement was written correctly. I do not know where this issue is located at all besides the very first if statements and elseif statements, which is the problem.

Now, I already tried some solutions. The first one was detecting if the SpecialEffects parameter was nil or not, but the first if statement would NOT run at all regardless, despite there being a table, which makes the parameter not return nil.

For more info, these are the types of tables I am using for the currently built in if statements.

Current Usage of Tables

local AllowedSpecialEffectsTable = {
	["Force"] = true; --1
	["Grab"] = true; -- 2
	["Damage Overtime"] = true;-- 3
	["Spawn Extra Attacks"] = true; --4
	["Lifesteal"] = true; -- 5
	["Reduce Damage for Specific Instances"] = true; -- 6
	["Deal Percentage Damage"] = true; -- 7
}

--\\// Reduce Damage for Specific Instances \\//-- --->

	local Table = { 
		{
			Name = "Table of Instances"
			Instances = { -- Required
				[""] = true; --Enter any name here
			}
		}
	}

--\\// Deal Percentage Damage\\//-- --->

	local Table = { 
		{
			Name = "Deal Percentage Damage";
			DivideMaxHealth = division;
			MaxHealthDamage = DivideMaxHealth/division; -- Divide "DivideMaxHealth with whatever"
		}
	}

I’m out of options. If anybody would like to come help, please do so. Tables in Roblox confuse me the most because of their not-very descriptive nature. I am pretty sure I wrote most of my code correctly, but there is some issue I have yet been able to resolve as a result of the typeof(SpecialEffects) == "table" problem, which just defaults to printing a warning of the entire SpecialEffects table.

2 Likes

image

image

Here’s a little bit more visual proof. If I am not giving enough information and you would like to help debug, please respond as soon as possible.

1 Like

From the given information about the AllowedSpecialEffectsTable, using numerical indices wouldn’t work because it’s a key-pair dictionary (not an array).

The first check should look like this:

if typeof(SpecialEffects) == "table" and table.find(SpecialEffects,AllowedSpecialEffectsTable["Reduce Damage for Specific Instances"]) then

And so on and so forth.

You would only use [6] if the table looked like this:

local AllowedSpecialEffectsTable = {true, true, true, true, true, true, true}

The never type is produced if the given check results in a type that wouldn’t exist or making sense:

local a = "string"
if type(a) == "string" and type(a) == "number" then
    -- 'a' is seen as 'never' here
    print(a) -- this will never print
end

Well actually, looking at this again, did you mean to write this:

table.find(SpecialEffects,"Reduce Damage for Specific Instances")

?

So I was finding a value in a table which was a dictionary all along incorrectly. I did massively change the code in my ModuleScript so everything is completely different now, which completely works.

Thanks for helping though! I fixed the code on my own for both of the current functions that work. Sorry if I wasted your time unforunately. I changed the AllowedSpecialEffectsTable to accept strings and not key-pairs, separated the elseif statements into local functions and made other local functions that check the value of a table’s indexed value name, check the name of a table’s property, and the value of the table.

Although I still have some questions. What is the difference between key-pair dictionaries and tables?

Well, they’re the same thing technically. In Lua/Luau, there aren’t “dictionaries” or “arrays;” just tables. However, people (like myself) like to make a distinction between the types of tables being discussed or analyzed.

A dictionary would look like this:

local dictionary = {
    key = "value",
    intIndex = 10
}

There’s a provided key and value for each of the table’s entries (for example, the table or math libraries: they’re string indices with function values).

An array looks like this:

local array = {1, 2, "a string value", 4, false}

Each entry is just a value. The index is automatically created by Lua/Luau, and it’s a number pointing to the value’s position in the table (i.e. array[1], array[2]).

(You can also define it yourself if you’d like)

local array = {
   [1] = 1,
   [2] = 2,
   [3] = "a string value",
   [4] = 4,
   [5] = false
}

Essentially, for arrays, the indices are all ordered numbers, while dictionaries do not follow that.


If you’re still confused, Roblox has some articles on them:

2 Likes