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

How to give a dictionary (tables) a value if it's set using a variable?

Sorry for the weird title but I cannot explain it better, I can just show it to you.

local Table = {
	['Apple'] = 10
}

local Target = 'RedcommanderV2'
local TargetKey = 1000

print(Table.Apple)
table.insert(Table, 'Banana')
Table.Banana = 11

table.insert(Table, TargetKey)
Table.TargetKey = Target

while wait(2) do
	for i,v in pairs(Table) do
		print(i..' '..v)
	end
end

As you can see, I'm trying to use this

table.insert(Table, TargetKey)
Table.TargetKey = Target

To set the value to a string value

The problem is that it would create a new value named TargetKey, that's not what I want, I want the value of TargetKey (I inserted the value into the table as you can see) to be equal to the string name

(This script is just for testing, it's not a real script I use)

Is there any way to do this?

1 answer

✓ Accepted answer

Is this what you were looking for? Table[Key] = value

local Table = {
    ['Apple'] = 10
}

local Target = 'RedcommanderV2'
local TargetKey = 1000

Table['Banana'] = 11
print(Table.Banana) -- 11

Table[TargetKey] = Target
print(Table.TargetKey) -- nil
print(Table[1000]) -- RedcommanderV2

for i,v in pairs(Table) do
    print(i..' '..v)
	-- Apple 10
	-- 1000 RedcommanderV2
	-- Banana 11
end

4 comments

0

See you're right, it works, but like you can see , the index is behind apple and banana, but it's 1000 redcommanderv2 though, why is it like that, isn't there a way to do it the same as a and b?

0

I'm not sure what you mean by "a and b" Do you mean in a certain order?

0

No, like you see, lines 17 and 19 print the variable and then the index, line 18 does it reversed

0

So you're saying it should print "RedcommanderV2 1000" ?

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