I have a ModuleScript that adds a button. It works only once, when I try to require it again, nothing happens. How to fix this problem? Expect stupid mistakes.
ModuleScript:
local module = {}
function module.insert(target)
local part = script.Button
part.Parent = game.Workspace
part:MoveTo(game.Players[target].Character:FindFirstChild("HumanoidRootPart").Position)
end
return module
when you require a module script, it will only run the first time
local module = {}
print(5)
function module:func()
print("func called")
end
return module
then in a local script
local m = require(module) --prints 5
local m = require(module) --will not print anything
m:func() --prints "func called"
m:func() --prints "func called"
What Ocipa explained is correct and useful information, but the problem is not that module scripts are ran once. Even though they’re run once, their returned value gets cached so the next time you call require on it again it should return the same table.
require(workspace.ServerScriptService.Module).insert(somePart)
require(workspace.ServerScriptService.Module).insert(somePart) --both should work
So if you require it again, the same function should work. And your module isn’t doing anything besides that function definition so i’m not sure why you thought it was ran once. The problem is with the script using the module’s functions. Can you send it please?
Well I just realized you’re parenting the same button (the one under the script):
local part = script.Button
part.Parent = game.Workspace
so the next time you call it, the same button would not be there (unless you have multiple buttons named “button”), which would also result in an error I assume which you could’ve supplied. Perhaps you wanted to :Clone() the button before reparenting.