ModeString = {"Normal", "Colored"} ModeValue = {_G.normal, _G.colored} function PickMode() local nextmode = math.random(#ModeString) Talk("The gamemode is ".. ModeString[nextmode],2, "") ModeValue[nextmode] = true end function ResetMode() for i,v in pairs(ModeValue) do v = false end end
This is a portion of a functioning script. These functions, however, aren't working entirely.
The non-functioning portion is where I try to set a global variable as true
ModeValue[nextmode] = true
The code works perfectly up until this line. It seems to skip this line without displaying an error in the output.
Also, ResetMode()
obviously doesn't work; I used the same thought process as the line above.
Is there an error in this code, or is this simply not possible?
Changing v
changes v
. v
is not linked up to the corresponding thing in the table, or where it came from.
The same is said for how you use _G.normal
. The list doesn't remember where the expression came from. It'll simply drop a false
or true
in there. Modifying ModeValue[nextmode]
modifies ModeValue. You didn't tell it to change _G
so it won't.
For ResetMode
:
In order to modify ModeValue
, you have to explicitly reference it:
ModeValue[i] = false
For PickMode
:
The simplest way is to store the name:
ModeValue = {"normal", "colored"} ---- _G[ModeValue[nextmode]] = true
however at that point you might as well just use ModeString
instead.
Is there a particular reason you're using booleans in _G
instead of something more controlled, e.g., a list of modules to run, for example?