Attempted to call require with invalid argument(s)

hello everyone, I am having trouble requiring a Module script within a model. I am getting the error "
Attempted to call require with invalid argument(s)", I am unsure of why. This is my script:

local Stages = game.ReplicatedStorage.Stages:GetChildren()
local Stage = Stages[math.random(1, #Stages)]:Clone()

if Stage:FindFirstChild("Start") then
		local Start = Stage.Start
		local Req = require(Start)
		Req.Start()
	end

there very clearly is a “Start” script within the model, as you can see here:
image

I would like to know why it is telling me Start is invalid. Thank you in advance!

You’ve forgotten to parent Stage.

The following works for me:

local stages = game:GetService("ReplicatedStorage"):FindFirstChild("Stages"):GetChildren()
local randStage = stages[math.random(1, #stages)]:Clone()

randStage.Parent = game.Workspace

require(randStage:FindFirstChildOfClass("ModuleScript"))

print("done")

With the architecture:

image

Hope this helps!

I parented it in a different script, I can paste it here if you want. This script is solely for initializing the stage specific script.

Within this script, you clone an object:

-- Accesses a random instance in the Stages table and clones it
local Stage = Stages[math.random(1, #Stages)]:Clone()

After that, Stage only exists in memory as a reference to a copy of an object; it doesn’t exist as an object in the game.

You need to parent this after cloning it before accessing any of its children, similar to my script.

Hope this helps!

I will try this when I get home. If this works, I will mark it as the solution!

1 Like

I think the problem was that I was attempting to index the module script as just “Start”. When I index it as Stage:FindFirstChildOfClass("ModuleScript") instead of Stage:FindFirstChild("Stage") it works fine. Thank you for helping, I am giving you the solution as I would not have been able to find this without you helping me!

1 Like