How would I get the boolean name instead of the value?

I have no lines of code for anything because I have no clue how to do this or even if it is true, basically I’m trying to get the boolean name and not the value, any help?

example:

local dog = true
--i want to get "dog" and not true

Why though? There’s no need for this when you can just use a dictionary if you need the index name.

1 Like

basically I have a set of values, for example:

dog = true
cat = false
orange = 10

and I’m saving data with one short table,
{
“dog true”,
“cat false”,
“orange 10”
}
then im using string.split to get the 2 values seperated by space apart, meaning i now have dog and true as an example
basically i want to check if the boolean name is the same as the first value i get, so dog and dog without having to hardcode it

edit: I already have multiple values spread out across multiple scripts and I don’t want to change the set of values to a table, it would just be annoying

well if your using string.split, shouldn’t you get it like:

local tbl = {
	"dog true",
	"cat false",
	"orange 10",
}

local function GoThroughList()
	for i,v in pairs(tbl) do
		local str = string.split(v, ' ')
		if str[2] == 'true' then
			print(str[1] .. ' is true!')
		elseif str[2] == 'false' then
			print(str[1] .. ' is false!')
		else 
			print(str[1] .. ' is some other value')
		end
	end
end

GoThroughList()
--oldvalue is dog true
local namevalue = string.split(oldvalue, " ")[1] --dog
            local value = string.split(oldvalue, " ")[2] --true
            print(namevalue.." "..value) --"dog true"

im just doing this but going through the whole table

are you trying to change the “true” string into a boolean?

in that case use some sort of function like this:

local function StringToBool(str)
	local b = {['true']=true;['false']=false}
	return b[str]
end

print(StringToBool('true'))
print(StringToBool('false'))
print(typeof(StringToBool('true')))

-----

if StringToBool(value) then
    print('true')
else
    print('false')
end

image

Basically I have a value like this:

dog = false

When the player leaves the game the value gets inserted into a table that will save like this:

{
"dog true"
}

When the player rejoins I want the value to become the saved value in the table without having to hardcode it

Basically original value being false and the saved value being true and turning the original value into the saved value

I just want to check if the first value in the saved table (dog) is the same as the values name (which is also dog) then set the saved value as the value of the boolean (sorry if I wrote it confusing)

still not sure if i understand you completely, if i read you correctly you would want to do something like:

local original = {
	dog = false
}

local new = {
	"dog true"
}

local function StringToBool(str)
	local b = {['true']=true;['false']=false}
	return b[str]
end

print(original, new)

for i,v in pairs(new) do
	local namevalue = string.split(v, " ")[1] --dog
	local value = string.split(v, " ")[2] -- true
	original[namevalue] = StringToBool(value)
end

print(original, new)

image

the code compares the original value to the new value, and updates the original value to the new value. If i am still wrong please correct me :sweat_smile:

You do get what I’m saying its just the original value isn’t in a table like you show, so is it possible to do the same thing you are doing but the original value isn’t in a table and its just a boolean value?

in that case, I believe you should just rather but the original value in a table it just makes way more sense. Not sure there is a way to call the variable without it being in a table anyways.

okay I’ll just do that, but thanks for helping

1 Like

You may be able to use setfenv but I would recommend against it in favor of something like @Towphy suggests

local namevalue = string.split(oldvalue, " ")[1] --dog
local value = string.split(oldvalue, " ")[2] --true

setfenv(0, {namevalue = StringToBool(value)})
1 Like