How to change variables outside of a function

Hello.

I would like to figure out a way to change variables that are inside of a function, or make a variable that can vary.

I want to do this because then, I could use the same function for changing Different things.

Right now, I could just make many functions but with different variables, but that seems to be an ineffective way of doing it.

2 Likes

Could you be more specific?

Here’s an example of something you may be wanting using a BindableEvent:

-- Script1

local add_amount = 1

function add(n)
   return n + add_amount
end

BindableEvent.Event:Connect(function(x)
  add_amount += x
end)
-- Script2

function increase(x)
  BindableEvent:Fire(x)
end

Whenever the BindableEvent is fired by the invocation of the increase function in Script2, the variable add_amount in Script1 will increase by the value sent through the increase function. You are essentially communicating across multiple scripts.

You can also use a ModuleScript or a IntValue to achieve the same effect; there are multiple ways to approach this.

Yeah so basically, I got a module script with scripted events.

One of these events is a part that appears when the function is fired, and disappears when the part is on screen (working).

However, creating a million functions with the only difference being the CFrame value, seems inefficient and messy.

edit: math.random is not the answer because I want the part to be in specific coordinates, not just random ones.

can you go a bit more in depth or perhaps show some code snippets so we can understand you better, pz

you can pack variables if this is what you mean:

local function something1(t)
      local cf1, cf2, cf3= t.cf1, t.cf2, t.cf3;
    
end
something1({cf1 = CFrame.new(), cf2 = ..., cf3= ...})

if you mean global variables then:


local variable = nil

function something1()
  variable = 1
end
function something2()
  if variable then print(variable) end
end

something1()
something2()

something1() will update the global variable for all other functions to see… these are my 2 guesses to your question…

if not, please provide more specific information regarding your question…

2 Likes

Hey, i’ll check this code out, I think this could be the solution i’m looking for.

I had no idea you could store variables. I’ll analyze this code now and see how it could be used and applied in different scenarios.

If I get it to work, i’ll follow up.

just to clarify further for you, you can just not send variables you won’t use from that specific point of calling your function aka:

local function something(info)
     local cframe = info.cframe
     if info.DelayedCFrame then
          task.wait(1)
          part.CFrame = cframe
     else
          part.CFrame = cframe
     end
end
something1({cframe = cf, DelayedCFrame = true})
--or
something1({cframe = cf})

this way you can use only what you need in functions without extra arguments or complications…

Programming in Lua : 5.2 for more information on variadic functions, which are functions that can accept a variable number of inputs