Ad
Log in to vote
0

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

Asked by 6 days ago
Edited 6 days ago

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

Log in to vote
1
Answered by
imKirda 2679 Moderation Voter Community Moderator
6 days ago
Edited 6 days ago

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
0
so is there another way to find what's inside of a table and to print out Character = { PointsRequired = 0, }? Eric_pokemon 134 — 6d
0
You can also use next or pairs loop for that, however just indexing it like Skills["Character"] is a lot more efficient. imKirda 2679 — 6d
0
ok i'll try it out Eric_pokemon 134 — 6d
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 Eric_pokemon 134 — 6d
View all comments (3 more)
0
oh i see, indexing tables is same as indexinng Instances, so just do Skills[TargetName] without quotes. My bad i explained horribly. imKirda 2679 — 6d
0
ok Eric_pokemon 134 — 6d
0
Thank you Eric_pokemon 134 — 6d
Ad

Answer this question