Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Table value is always nil, no matter what?

Asked by 5 years ago

Hey guys I have a problem that is starting to get me really really frustrated. I've been trying to fix this script for about 1 hour now trying different methodes but It never worked and I have NO IDEA what I'm doing wrong.

Here's the simple script:

local OFFICIALPerksTable = 
{
PowerEngagement = "Very Rare"
}


 local Rarity = Instance.new("StringValue",game.Players.LocalPlayer:WaitForChild("Items").Perks:FindFirstChild(v))
    Rarity.Name = "Rarity"
    local TheStringValueWeWant = v
    print(v) --Result: PowerEngagement (It's exactly the scring value I need)
    Rarity.Value = OFFICIALPerksTable.TheStringValueWeWant

The script seems simple right? Supposedly it should read the OFFICIALPerksTable and return "Very Rare" from "PowerEngagement" (The string that is given).

Instead, it always returns nil. I tried tostring(), tried a for loop, tried changing the table, changing names, locations for 1 h and nothing worked. It would either give me a error saying that the value is nil or will set the Rarity value as "nil"

This is really pissing me off and I don't understand why this is happening. I'm at the limits of my frustration. Some help would really make my day way better without having to deal with this ROBLOX NO SENSE LOGIC. Thanks.

0
change it to: ["PowerEngagement"] = "Very Rare"; awesomeipod 607 — 5y
0
refer to it like this OFFICIALPerksTable["PowerEngagement"] awesomeipod 607 — 5y
0
I'll try and see the results wilsonsilva007 373 — 5y
0
Still nil :/ wilsonsilva007 373 — 5y

1 answer

Log in to vote
1
Answered by
tantec 305 Moderation Voter
5 years ago
Edited 5 years ago

What you're using is a dictionary, not a table. It is important not to get mixed between the two. Also I believe you've forgotten to use an in pairs function.

local OFFICIALPerksTable = 
{
PowerEngagement = "Very Rare"
}

for key,value in pairs(OFFICIALPerksTable) do
    local Rarity = Instance.new("StringValue",game.Players.LocalPlayer:WaitForChild("Items").Perks:FindFirstChild(key))
    Rarity.Name = "Rarity"
    local TheStringValueWeWant = key
    print(key) --Result: PowerEngagement (It's exactly the scring value I need)
    Rarity.Value = OFFICIALPerksTable.TheStringValueWeWant
end

Also in dictionaries when you're doing for in pairs functions instead of the number going first like with tables right here..

table = {"ok", "dont troll please", "i have no life"}
for i,v in pairs(table) do
    print(i) -- would print the number position in the table it's in
    print(v) -- would print the thing
end

It would instead be like this..

dictionary = {
    mynanabusesme = 20 -- the key would print 20
    sosboss = {"crazy facts"} -- the key would be the table so like key[1] would print "crazy facts"
}
for key, value in pairs(dictionary) do
    print(key) -- would print out 20
    print(value) -- would print out something like table#281ABSC or something like that
end

~tantec

0
Thanks. You made me notice the stupid mistake I made. wilsonsilva007 373 — 5y
0
Actually printing the key of the dictionary will print its name, not its value. Since sosboss is a table, it prints its id. But printing key would be mynanaabusesme. But do use local variables and choose more appropriate variable names. User#19524 175 — 5y
Ad

Answer this question