I’m trying to be more concise in my coding, but I’m hitting a snag. I’m attempting to concatenate a couple strings together, with the result being the name of a previously called Variable.
Example:
local itemsTable = {"cat", "dog", "money", "tree"}
local dogName = script.Parent.dogLabel -- A StringValue
dogName.Value = "None"
for i, v in ipairs(itemsTable) do
if v == "dog" then
local newName = (v .. "Name")
newName.Value = "Fido" -- Error here: attempt to index string with 'Value'
print(newName.Value)
end
end
Is there another way for me to call the pre-made variable by concatenating strings?
edit : sorry I see the problem; your trying to index the name you concatenated with Value, your not actually pointing to the object’s value @Honkeysimons
Im not sure if this is exactly what you want but here
local itemsTable = {"cat", "dog", "money", "tree"}
local dogName = script.Parent.dogLabel -- A StringValue
dogName.Value = "None"
for i, v in ipairs(itemsTable) do
if v == "dog" then
local stringValue = Instance.new("StringValue")
stringValue.Name = (v.."Name")
stringValue.Value = "Fido"
print(stringValue.Value)
end
end
I have different StringValues and table values that I want to bring together to form the names of Variables. In that way I can reference the variable in a loop.
Alright this should work, nice and clean! @Honkeysimons
local itemsTable = {"cat", "dog", "money", "tree"}
local dogName = script.Parent.dogLabel -- A StringValue
dogName.Value = "None"
for i, v in ipairs(itemsTable) do
if v == "dog" then
local newName = script.Parent[(v.."Name")]
newName.Value = "Fido" -- Error here: attempt to index string with 'Value'
print(newName.Value)
end
end
You can actually achieve this by doing something like this:
local itemsTable = {"cat", "dog", "money", "tree"}
dogName = script.Parent.dogLabel
dogName.Value = "None"
for i, v in ipairs(itemsTable) do
if v == "dog" then
local newName = getfenv()[v .. "Name"]
newName.Value = "Fido"
print(newName.Value)
end
end