ModuleScript: Works Only Once

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
1 Like

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"

to learn more about module scripts, read this
https://education.roblox.com/en-us/resources/intro-to-module-scripts

1 Like

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?

2 Likes
require(5864551347).insert("Your_Name")
1 Like

What happens exactly after requiring it and calling the function again?

Literally nothing. New button is not creating.

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.

2 Likes

I just forgot to do the thing I always do… Well I warned you about stupid mistakes :slight_smile:

1 Like