Is there a way to make the script the name of a varible?

imagine a varible ok?

local I_Love_My_Family= true

Is there a way to make the script get the varible name which is i love my family?

2 Likes

what are you asking exactly? also why is your variable named that?

2 Likes

Can you make it clear? I don’t really understand what you’re asking for.

2 Likes
-- Have a variable named "i love my family" in workspace.Folder
-- a bool in this case set to true

local children = workspace:WaitForChild("Folder"):GetChildren()
for i = 1, #children do
	local child = children[i]

	if tostring(child) == "i love my family" then
		print(child)
		print(child.Value)
	end
end

output:
i love my family
true

2 Likes

If I understand your question correctly then you can use table for this, here is example (actually I still confused even after re-read the post)

local list_of_variable = {
I_Love_My_Family = true,
I_Love_My_Friends = true
}

--Somewhere on script
print(list_of_variable.I_Love_My_Family) --true

--Usage in pair loop
for variable_name,value in list_of_variable do
--Do something with variable name
print(variable_name)
print(value)
end
3 Likes

Sorry for being late i forgot about this post and devfourm didn’t give me a notification
Im asking how to make the script get the name of a varible
I’m talking about the text that is after the word local

2 Likes

Ehh im asking because im kinda lazy to print

print("The Amount of Money = "..Money) 

Instead of making a function that tells me what varible was that when checking for bugs

2 Likes

Because i love my family of course :man_shrugging:

1 Like

local I_Love_My_Family= true

print(I_Love_My_Family)

if your asking for another script to access it i would recommend using a module script and require since atleast for me it works better:

2 Likes

Not possible. Variable names are there for the developer’s convenience. Internally, Lua does not care about those names as plain text, it’ll just make references (in a not-plain-text way) to the values. The closest you can get is by using a dictionary (table with string keys).

local variables = {
	I_Love_My_Family = true,
}
local variableName = "I_Love_My_Family"
print(variables[variableName])

Now that I realised, why do you want to get the variable’s name when you’ll be typing it in the editor anyway? Just asking.

2 Likes

Pure laziness that i want to make a function that prints varible…variblename just to save 5 characters in the script while testing

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.