Gui loading require scripts

I want to make a script that loads a require script when a button is pressed with given values.
I tried to make the script when the button is pressed be:

id = script.Parent.Parent.Id.Text
plr = script.Parent.Parent.Player.Text
require(id).load(player)

And yes it’s a normal script, and the code doesn’t give any errors but it doesn’t do anything either.

The id is a string type that gets the name of the module. To use require() you must reference the place where the module is. For example:

local Module = require(game:GetService("ReplicatedStorage")[id])
Module.load(plr)

The module is a model in the library, does that change anything?
The library I mean is what’s shown in the toolbox.

Oh do you mean that id is the roblox id of the module? In that case this is all you have to do:
local Module = require(tonumber(id))

So how would I make that for a specific person because the script I’ve been using without the variable is:

require(123).load("ShadowAlien98")

to load things like guis.

Yep, that should work. Make sure if the modulescript needs the player name or the player instance itself. If it needs player name, that should work. Otherwise, get the player object from Players service and send it in as a parameter.

So as a normal script and not a local script:

function leftClick()
id = script.Parent.Parent.Id.Text
local Module = require(tonumber(id))
end
 
script.Parent.MouseButton1Click:Connect(leftClick)

and it has to have the player name

You should use a local script to get inputs from the UI. To make this work, you would have to fire a remote to the server asking to require the specific module.

So why when replacing the id part of the require it breaks? And only the id and not player name.

What do you mean? Could you elaborate on the problem.

When I use

require(123).load("ShadowAlien98")

it works
When I use

id = script.parent.parent.text
require(id).load("ShadowAlien98")

nothing happens, maybe because the id is entered into a textbox?

In short, both of those are different data types.

For the first one, when you do require(), you pass in a number as an int value. For the second one, you pass in a string value instead of an int. So to fix this just convert the string to int like int = tonumber(string).

So this?

id = script.parent.parent.text
require(tonumber(id)).load("ShadowAlien98")

Yes, that would work because you converted it to a number instead of passing it as a string.

Ohhh, that explains it, thanks for the help.

Uh

 Attempted to call require with invalid argument(s).

When I run

script.Parent.MouseButton1Click:Connect(function()
	id = script.Parent.Parent.ID.Text
	require(tonumber(id)).load("ShadowAlien98")
end)

tonumber returns nil if its argument could not be converted into a number. So something like 4959s would return nil since there is a non-number (the letter “s”).

And keep in mind that you can’t require by ID on the client, only the server can. Are you sure you need to do it this way? Why not just import the module into your game so it can be used without requiring by ID?

I’m using a normal script, not local, and it’s just numbers with no strings.

You shouldn’t be using a server script to handle UI either, you should probably use a remote event instead.

Try print(id, tonumber(id)) before requiring just to be 100% sure though

So how would I go about doing that?

1 Like