I’m getting a problem where when I die, and put a print inside a module, it’s printing twice (onces for the new require, once for the old) and the old require ends up erroring out.
local Buttons = {}
local CurrentButton = 1
Title.Text = 'Choose gamemode'
for _, v in pairs(Choices:GetChildren()) do
v.Visible = true
v.Size = UDim2.new(0.85, 0, 0.85, 0)
if v.Name == '1' then
v.Position = UDim2.new(0.15, 0, 1, 0)
elseif v.Name == '3' then
v.Position = UDim2.new(0.85, 0, 1, 0)
end
table.insert(Buttons, v)
end
print(Buttons)
print(#Buttons)
So the prints
print(Buttons)
print(#Buttons)
First time printing
table
3
Then when I die, and respawn it prints
table
3
table
0
Which the table 3 is the new module require, table 0 is I’m guessing the old module require. How can I stop this problem from occuring??
Put this code inside a method then call it when CharacterAdded fires as opposed to leaving it in the topmost scope. Leaving the code in the topmost scope should only be done for initialisation features that reasonably would need to be done only once ever when the module is required. Everything else should be done in functions.
I don’t understand your reply at all. What difference does this make? The contents of StarterGui are cloned over to PlayerGui, so you would have to reference PlayerGui regardless. As for what I’m saying, I mean exactly what I said: put your code in a function instead of having it run implicitly upon require and call that function each time it needs to spawn up again. You can’t “cancel” or “reload” modules.
I never said anything about StarterCharacter, I said to move the contents of the ModuleScript that aren’t in a function, into a function and then call that each time the Gui needs to reset or refresh something. The thing to understand here is that you aren’t “resetting” things, you’re accounting for respawns by adapting your code as so.
For example: you have a non-resetting HealthGui that also makes use of a ModuleScript and both items are under that Gui. In that case, you account for new character spawns using CharacterAdded so that your module isn’t referencing old items.
Outside of a function, your ModuleScript is still recognising the first Gui because that’s what’s indexed upon require. There’s no functions or CharacterAdded accounting anywhere so each require will always reference the same old Gui. Therefore you need to incorporate this code into a function that can be ran when a new character is added.
Take the PlayerModule and its controls for example. The PlayerModule is found in a non-resetting container, PlayerScripts. How is it able to then allow the client to control a new character every time it respawns without any weird tricks? It doesn’t reset itself, it accounts for new characters. See:
The following linked function is called when CharacterAdded fires. It is responsible for hooking up certain functionality to the new character, so that the current character and any new characters after respawn share that functionality. For example, at the very top of the function, it updates the Humanoid variable so that the ControlModule is acting on the current Humanoid rather than an old one.