Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

(<- Random -1 FTW :3) Would a module script work in this situation? [Not solved]

Asked by 10 years ago

Lets say I have a value called number

number= 3

How do I change this with another script? Things I want to bypass doing -Value Objects (They lag) -_G variables (Because that would affect everyone in the server, right? this value is for each individual player, script would be in their character)

So I was thinking about approaching this with a module because I also have functions I would like to call with other scripts for it to be more organized. Thing is, I dissected module scripts and the wiki, but I don't know how to approach it in this situation. Let me give an example function that would be in the module.

function arm(WELD)
    wlarm.C0 = CFrame.Angles(math.rad(WELD[1]),math.rad(WELD[2]),math.rad(WELD[3])) + wlarm.C0.p
end

the variables WELD[1],[2],[3] are number holders, 1 is a x value 2 is Y and 3 is X I dont know whether or not to return something to the script that called the function. Because in this function, it just rotates parts, not sure if modules can do that. Some clarity would help :)

Thanks

2 answers

Log in to vote
1
Answered by 10 years ago

There is a possibility you could use Object-Oriented Programming to develop a system where each individual has their set number within a meta-table. This system would be developed within a Module and all data would have to be stored in a specific area.

Another option would be to use RemoteEvents/Functions for Client/Server communication if you want the server to have the ability to update detail for an individual Client.

You could also create a IntValue within the Player (userdata), this would allow both server and client to update 'stats' of an individual client.

Ad
Log in to vote
0
Answered by 10 years ago

If you have multiple functions you want a ModuleScript to return. You will need to return a table (due to a ModuleScript only being able to return a single value). Keep in mind that a ModuleScript will only run it's initial code once and will recreate the object it returns every time a script requires the returned value.

Keeping all of this in mind, you could create your initial values at the beginning of the ModuleScript and create an interface that is returned by the ModuleScript for other Scripts to use to manipulate and access those values:

local interface = {}
local number = 3 -- You should always make code local if you can, it is much faster to interpret.

-- Lua will automatically return nil if a function does not explicitly return a value.
function interface.arm(WELD)
    wlarm = workspace.SomePart -- If this is a utility module, it may be worth passing in the part to weld as a separate parameter (the function would be: arm(WELD, thePart))
    wlarm.C0 = CFrame.Angles(math.rad(WELD[1]),math.rad(WELD[2]),math.rad(WELD[3])) + wlarm.C0.p
end
function interface.setNumber(x)
    number = x
end
function interface.getNumber()
    return number
end

return interface

Follow Up

That's what I'm confused about. do I have to place local interface = {} and return interface? The model just executes the rotation, the script calling the function is just requesting that the rotation be executed. One more question, if I call two functions right after another in the requesting script. would the second line wait till the first line function has been completed?

What you return is up to you. You can return any value you want as long as you only return a single value (you must return at least one value, even nil will count as a return value). I am simply using this interface as a table to hold multiple functions to be returned.

As for your next question, a thread is responsible for executing code in lua, each Script gets it's own thread and will run it's given code in the order it is given. A function can call multiple functions, whoever the function has to wait for the called function to return before continuing on to the next function call. You can call multiple functions at the same time by creating another thread (lua has a library called coroutine for this reason). Whoever the order in which the functions are called is not explicit. I would highly recommend looking into Lua threads and the coroutine library. They are highly advanced subjects, whoever learning them can be a huge asset.

0
That's what I'm confused about. do I have to place local interface = {} and return interface? The model just executes the rotation, the script calling the function is just requesting that the rotation be executed. One more question, if I call two functions right after another in the requesting script. would the second line wait till the first line function has been completed? Orlando777 315 — 10y
0
Is there a reason why the local script gives me an error saying numbers isnt indexed and thefore == nil? Im simply writing (Module name).number after I required it. Orlando777 315 — 10y
0
If you used the code I gave, the interface should not have a number index. You will need to use the getNumber and setNumber to access this number. Also, be sure to use the require function with the module to access its return value. P.S. Sorry about the very late response. cjcool12345 58 — 9y

Answer this question