How would i get variable name instead of the variable value?

Making a debounce function with the callback, i need to figure out how to actually get the variable name.

function DebounceBool(boolvar,bool,callback)
	local boolvariable = boolvar -- this thing actually says value 
								-- instead of the variable itself
	repeat
		callback()
		print(tostring(boolvariable)..' Debounced')
	until boolvariable == bool
	boolvariable = bool
end
1 Like

You can show the full script? or the script that calls that function?
I recommend to do another variable to the function called “boolname” so something like that, that variable storages the variable name

if debounce.attacking == false and entity.Humanoid.Health > 0 then
			if math.abs(relativePos.X) <= npc.Hitbox.Size.X * 1 * 0.6 and math.abs(relativePos.Z) <= npc.Hitbox.Size.Z * 1 * 0.6 then
				DebounceBool(debounce.attacking,true,function()
					local atktimer = stats.atktimer
					local idletimer = stats.idletimer
					local length = stats.lengthofAtk
					animstring = 'atk'
					debounce.walking = false
					task.delay(atktimer,function()
						onHit()
					end)
					task.delay(length,function()
						debounce.atkended = true
						debounce.walking = true
					end)
					task.delay(idletimer,function()
						debounce.atkended = false
						debounce.attacking = false
					end)
				end)
			end

		elseif debounce.atkended == true and math.abs(relativePos.X) <= npc.Hitbox.Size.X * 1 * 0.6 and math.abs(relativePos.Z) <= npc.Hitbox.Size.Z * 1 * 0.6 then
			animstring = 'idle'
		end

As expected it makes the bool false and the script wont spam.
In reality it says “true = true” then spams the callback

dont ask why i made this function cause i got lazy with if debounce == false then

You can’t, but because you already know the variable’s name to type it in, you could do;

function DebounceBool(boolvar,bool,callback, debugname)
	repeat
		callback()
		print(debugname..' Debounced')
	until boolvar == bool
end

And call it like this;

DebounceBool(MyBooleanVariable, true, somefunction, "MyBooleanVariable")

Aw it would be interesting if we could get the variable name instead of the value…

There are other problems with doing so, such as it being easier to make exploits etc.

Why would you need to get a variables name when… you need it in the first place to even use the variable?

You can if you declare variables globally and use the function environment functions; getfenv, setfenv to get/set them.

s = "Hello world!"
local env = getfenv(0)
for k, v in pairs(env) do
	if v == "Hello world!" then print(k) break end --'s' is printed.
end
3 Likes

Interesting! Thanks!

s = {'bool1',true}
local env = getfenv(0)
for k, v in pairs(env) do
	if type(v) == 'table' then
		if v[1] == 'bool1' then
			print(k..' = '..tostring(v[2]))
		end

	end
end

-- s = true

Note than getfenv/setfenv will disable certain Luau optimizations.

cc: @Forummer you should probably add a note to your reply as well.

This is true, optimizations such as inline caching and global access chains are disabled for functions that have had their environments read/written, additionally, the loadstring global function will disable these optimizations. This deoptimization process can be mitigated by declaring globals as upvalues (non-local variables) to functions that have their environments read/written.

local Print = print --Integrate 'print' global function into local environment.
local Pairs = pairs
String = "Hello world!"
local env = getfenv(0)
for k, v in Pairs(env) do
	if v == "Hello world!" then Print(k) break end --'String' is output.
end

It should be noted that these optimizations are insignificant in the grand scheme of things.