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

Need help finding variable inside of a module script(dictionary/table)?

I'm currently creating a skill tree but when I try to find a variable inside of the dictionary and print it, it prints out "nil" and not what's inside of Character

local Skills = {
	Character = {
		PointsRequired = 0,
	};
	Skill1 = {
		PointsRequired = 1,
	};
}
Skills.putPoint = function(plr,Target,TargetName)
	local node = table.find(Skills,"Character")
	print(node)
end

return Skills

any idea why?

1 answer

Index keys like this

local node = Skills["Character"]
-- or
local node = Skills.Character

print(node)

It won't error but will return nil if the key is not in the table.

In your case are using table.find wrong, this function searches for value instead of key and returns position of the value inside of the table, it only works on values without keys, like this:

local Array = {
	"Cat",
	"Another Cat",
	"Another Another Cat",
	"Another Another Another Cat",
	"Giraffe",
	["oh"] = "no",
}

table.find(Array, "Cat") -- 1
table.find(Array, "Another Another Cat") -- 3
table.find(Array, "Gorilla") -- nil
table.find(Array, "oh") -- nil
table.find(Array, "no") -- nil

I also recommend you declaring functions like this instead as it's preferred syntax for that

function Skills.putPoint(plr, Target, TargetName)
	[...]
end

7 comments

0

so is there another way to find what's inside of a table and to print out Character = { PointsRequired = 0, }?

0

You can also use next or pairs loop for that, however just indexing it like Skills["Character"] is a lot more efficient.

0

ok i'll try it out

0

because i'm trying to find it using TargetName so i don't have to make it if TargetName == "Character" then Skills["Character"] end

0

oh i see, indexing tables is same as indexinng Instances, so just do Skills[TargetName] without quotes. My bad i explained horribly.

0

ok

0

Thank you

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