How can I use a function parameter up to date?

Hello, im working on a module script. And i got a problem about function parameter isn’t up-to-date. Like in my sample. I created coroutine thread for make “hey” variable will be 1, after 5 seconds. But function is can’t get this update. My question is; how can i get my function parameter up-to-date? If that’s impossible, make me aware of this.

I tried found an article about that,
but i didn’t found any.

local hey = 757

coroutine.resume(coroutine.create(function()
	wait(5)
	hey = 1
end))

function Test(parm)
	for i = 1, 10, 1 do
		wait(1.5)
		print(parm == 1)
	end
end

Test(hey)
print(hey == 1)
print(hey)

Output:
false(10x)
true
1

Thanks for whoever help.

NOTE: I know i can do it as print(hey == 1) but i will use this function at a module script.

1 Like

Are you talking about ByRef and ByVal?

If you are then there isn’t really a way to pass a value like that unless it is in an table I believe.

Here is a link to a previous post you can learn a bit there.

1 Like

Your problem is that you’re trying to verify a parameter that is not exactly what you want anymore.

For example, you’ve sent to the function “Test” the following value: “757” which was derived from the variable “hey”, what you might want to do is keep a variable in the global scope to keep a reference of it while doing the iterations (second example) OR assign the parameter to the updated value for every iteration of the for loop (first example), otherwise, “parm” will be received once unless you do one of these options:

First thing, I’ll be using coroutine.wrap() instead of resume and create, you can keep it the way you want, but understand that wrap will work the same.

Examples:


local hey = 757

coroutine.wrap(function()
    wait(5)
    hey = 1
end)()

local function Test(parameter)
    for i = 1, 10 do
        wait(1.5)
        parameter = hey -- You assign "parameter" to the "hey"'s value: variable we want to keep track of.
        print(parameter==1)
    end
end

Test(hey)
print(hey == 1)
print(hey)

OR


local hey = 757

coroutine.wrap(function()
    wait(5)
    hey = 1
end)()

local function Test() -- In this way we don't use any parameters, but since you wanted them, check the code above.
    for i = 1, 10 do
        wait(1.5)
        print(hey)
    end
end

Test()

The code wasn’t tested, but the way you want to really update the “parameter” is just to assign it to the variable you want to keep track of every iteration of the for loop so it’s always updated. (which is the first example)

I hope that’s what you wanted and that answers your question if not just reply and I’ll try to help.

OBS.: If you only have the function inside the module which I just read in notes, just create a global variable (in the module) and assign it from the script to the module using “module.hey = 1” while using the first example, then the parameter will update based in this variable which will be at some point updated, the thing is, there are many ways to do it, but that should do it.

EDIT: Fixed code’s indentation.
EDIT 2: Observation.

2 Likes

The problem is as I said, I will use that in a module script and I want enter specific parameter.

As @xXfilipejkXx said, there is a way to keep your function parameter. Just update it in every iteration.

Taken from @xXfilipejkXx :

local hey = 757

coroutine.wrap(function()
    wait(5)
    hey = 1
end)()

local function Test(parameter)
    for i = 1, 10 do
        wait(1.5)
        parameter = hey -- You assign "parameter" to the "hey"'s value: variable we want to keep track of.
        print(parameter==1)
    end
end

Test(hey)
print(hey == 1)
print(hey)

You can also use coroutine.create() with this.

I say this for the 3rd time. I will use this system in a module script with different objects. So, it’s impossible to make something like parameter = hey.

Just asking, have you readen my observation notes in my first reply?

There are a few solutions, you can create a global variable using either _G. or shared assuming both are in server or client - together, you will be able to update it “across” scripts, yet it’s not really recommended.

Another option for you would be as I already stated, instead of changing the variable of another script, just change the variable of the required module, let it be module_1 for that example.

-- // Script you said you'd be calling the function of. (different script)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local module_1 = require(ReplicatedStorage.Module) -- Change this to the module you're calling the function.

coroutine.wrap(function()
    wait(5)
    module_1.hey = 1
end)()

module_1:Test(757)

-- // Module Script
local Module = {}

function Module:Test(parameter)
    self.hey = parameter -- assigns value to variable inside the Module (starter value)

    for i = 1, 10 do
        wait(1.5)
        parameter = self.hey -- updates it in case the other script changed it
        print(parameter == 1)
    end
end

return Module

As I said before, there are multiple ways to do it , answers are based from what I understood from your question, if that is still not what you want I’d be grateful if you could explain further or give us some code examples of what type of usage you want to do with the module.

I hope that clarifies it, if you still have any questions I’ll try to help.

1 Like

I think I explain my problem wrongly.

I said I use parameter as different objects so, it’s not only for “hey” variable. Like; Test(MyVariable), Test(script.Parent) “As changed style, I know it will not work for this function” Its just a sample for explain my problem. I trying make a function named “WaitWhile()” in my project. But I didn’t posted that because it’s really complex.

I’m sorry for explaining my problem wrongly.

Hold on. What exactly are you trying to pass?

If it is a value across a script and a module script then you can simply update the ms value in the script.

Like so

-- Script
Module.Status = false
Module:SayStatus(10)
task.wait(5)
Module.Status = true

-- ModuleScript
--!strict
local Module = {
    Status: boolean = false
}
function Module:SayStatus(Time: boolean?)
    for Index = 1, Time do
        print(Module.Status)
        task.wait(1)
    end
end