Function not changing value for variable

  1. What do you want to achieve? Keep it simple and clear!
    I want this function to change the value of a variable.

  2. What is the issue? Include screenshots / videos if possible!
    It cannot change the value of the variable.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Deleting the value that are change and change again in the function.

local v1 = true
local v2 = true
local v3 = true

local function test(value)
 v1 = false -- deleting this fixed the issue
 v2 = false
 v3 = false

 value = true
end

test(v1)
print(v1) --Supposed to print true but doesn't.

When you send a function a value you actually duplicate the variable, that means value isn’t v1 but instead a completely different variable. That means doing value = true will not work since it will only modify value and not v1.

1 Like

You’re only changing “value” in the scope of that function.

If you want to change it outside with the same method you’re currently using, do instead:

local v1 = true
local v2 = true
local v3 = true

local function test(value)
 v1 = false -- deleting this fixed the issue
 v2 = false
 v3 = false

 value = true
return value -- return the variable you changed
end

v1 = test(v1) -- change the variable v1
print(v1)
3 Likes

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