How To Concatenate Strings into a Variable

Hello All,

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?

1 Like

What is the dogName?

like is it a string value?

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

Good call. Yes, a StringValue.

Update my post from before.
35chars

You cannot get the value of a variable. “NewName” is a variable so you cannot get the value of it.

Wait what are you actually trying to do anyway?

edit:

if your trying to make a string value then you have to use instance . new and create the object then reference its value to whatever you want

I don’t think you can convert a string to reference a pre-made variable

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
1 Like

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.

I think OP wants to reference a variable that he created before in the script by concatenating strings, e.g:

local dogName = script.Parent.dogLabel
local newName = ("dogName") -- should point to the dogName variable but it doesn't

Ohhhhh dude i see what your trying to do I get it, yeah this is possible ive done it in the past

edit: hold on

yea yea i get what he means nowww

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
3 Likes

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

image

3 Likes

@Honkeysimons Sorry change name to label and youll be all set bud

Wait @heII_ish could you explain what your doing

getfenv() returns the environment of the function, and since dogName is a global variable that the function has access to, you can get it from there

3 Likes

ohh alright that makes alot of sense, thanks

@Honkeysimons Alright you can mark my solution now :blush:

Thanks for the solutions guys! I appreciate it!

1 Like