I wanna keep my code clean so im just throwing all the functions i use alot in a module script, one function just updates the table of all players. by doing
Gamblers = game.Teams.Gamblers:GetPlayers()
but when i go and try to say in the main script
if #Gamblers == 1 then
it doesn’t have the updated info the module produces.
At the end of a module script, something is returned. Normally this is a class, but it can just be dictionaries, lists, etc. Nothing will return unless you specifically state it to, and it will only run once unless said otherwise. A fix for your problem would be to make it into a function assigned to the table you’re returning and then use that instead of just getting the players once.
As Benified said, modules should return one value, however do note that the module does run every time it’s required, only the returned value is shared between scripts, therefore I’d approach it this way:
local teams = game:GetService('Teams')
local gamblers = teams:WaitForChild('Gamblers')
local module = {
['Gamblers'] = gamblers:GetPlayers() -- this would be assigned the first time the module is required
}
module.playerAddedConnection = module.playerAddedConnection or gamblers.PlayerAdded:Connect(function(player) -- since we don't want to create a memory leak and we only want one person to be able to join one time ever, even if the module is required one time we have to check if the connection already exists, if it does then assign the existing connection to module.playerAddedConnection, otherwise create a new one
module.Gamblers[#module.Gamblers + 1] = player
end)
module.playerRemovedConnection = module.playerRemovedConnection or gamblers.PlayerRemoved:Connect(function(player) -- same as above
local index = table.find(module.Gamblers, player)
if index then
table.remove(module.Gamblers, index)
end
end)
return module
You also shouldn’t have to index it through the model because of instancing I think it’s called? Or serialization, idk but think of the table as an object/instance inside of the module. Since the table is an object whose properties are just the players, you won’t have to index it every time as it’s an actual reference to the table, not a clone of it.
local gamblerModule = require(module)
local gamblerTable = gamblerModule.Gamblers -- let's say at the initial declaration that the table contains 3 players, Player1, Player2 and Player3
-- let's say 3 minutes later Player1 and Player3 left the team
print(gamblerTable) -- should just print Player2, not Player1 or Player3
This is not the case. If by “run” you mean that variables and functions get declared/defined and other logic gets executed.
The second time (and every time after that) you require a module, you will only receive the (shared) return value, nothing else will happen. Which is handy. The only context in which that’s not the case is when you first require the module from the client and then from the server or vice-versa, just as all of the other module script behavior that doesn’t cross the client/server boundary.
If you don’t believe me you can test it out very simply:
Inside a module script add a print anywhere before return.
Require the module from two different scripts, or even twice within one script.
You will only get one print.
Or even more persuasive:
local module = {}
module.time = os.clock()
return module
Require the module from one script and print(module.time) after that.
Inside another script do task.wait(5) and then the same thing as in the first script.
You will see that the same time gets printed.