Value gets overwritten when using multiple ModuleScripts

Hello. I’m making a system where a user requires the module as so

require(moduleid)({
    plane = script.Parent;
    mainPart = script.Parent.PlaneObjects.Main;
    mainSeat = script.Parent.PlaneObjects.MainSeat;
})

However, if I require the module more than once the pre-loaded module’s values passed get overwritten by the other caller’s objects.

This is how I’m testing with multipule modules:

2 objects.

However, the 1st object loads correctly however, when the 2nd object loads the 1st object’s plane, mainPart, mainSeat objects are replaced with the 2nd object’s ones.

Sorry if this thread sounds confusing. If you need more info I can provide you with it

Is this any specific use case for this over making a specific function in said module that would add a plane to a table within the module script?
I only mention this because the “typical” way of using module scripts, at least how I use them, would be to require it once where needed and use functions within, rather than requiring it twice as you are.

1 Like

Thanks for the info. I shall try your way where I put the plane in a table and see how that goes. I’ll update this thread/post if it works.

Just one more note, you can put a table inside of the local myModule = {} it would work like so

local myModule = {
    planes = {},
}

Then you can make a function like this:

function myModule:addPlane(plane, mainPart, mainSeat)
    local planeTable = {plane, mainPart, mainSeat}
    table.insert(self.planes, planeTable)
end

This is just an example of you can go about adding it to your module script. Hopefully it gives you a little more insight on how to solve your problem. It’s also helpful to note that you should figure some way of identifying your planes so it is easier to remove them from the table as well, whether it’s returning the index of where it was added to the table, etc.

Note: I am on mobile and have not tested the above code.

2 Likes

Thanks for the guidelines. I’m currently going to identify each plane by giving it an unique id generated by HttpService:GenerateGUID

That’s a really good way of identifying them. I would suggest also storing that in the table, probably first under “planeId” or something of the sort.

1 Like