I am trying to get the function to connect to the chosen module script. The first half of the script chooses a random gamemode’s name from a folder under the main server script. The second half attempts to connect the selected gamemode’s module script which is the same object from the first half.
The error message shown is Argument 1 missing or nil
task.wait(10)
local GM = script.GameModes:GetChildren()
local chosenGM = GM[math.random(1, #GM)]
print(chosenGM)
local function StartGame(chosenGM)
task.wait()
local SG = script.GameModes:WaitForChild(chosenGM)
print(SG)
end
StartGame()
The problem could be that instead of taking the variable you’ve made on line 80, which has a value in it, It’s taking the chosenGM parameter. So just remove the parameter and it should fix it. (Or pass something through it)
The variable inside ChosenGM on the first half is what I want the function to use as the arguement. At least that is my understanding of it.
Currently the script:
Error message: Infinite yield possible on ‘ServerScriptService.MainScript.GameModes:WaitForChild(“Instance”)’
task.wait(10)
local GM = script.GameModes:GetChildren()
local chosenGM = GM[math.random(1, #GM)]
print(chosenGM)
local function StartGame(chosenGM)
task.wait()
local SG = script.GameModes:WaitForChild(chosenGM)
print(SG)
end
StartGame(chosenGM)
As others have said, that error is because your chosenGM global variable is being shadowed by the chosenGM argument in your StartGame function.
Fixing that, you’ll still have another error: you’re misusing WaitForChild here. That function takes a string, not an Instance, and GetChildren will return a list of the Instances already present. WaitForChild is effectively doing nothing, here.
Given that you’ve made StartGame a function, I’d hazard a guess that you intend for it to randomly select a gamemode every time you call it? If so, your code won’t work at all, as the chosenGM global variable is set once, and would be used every time the StartGame function gets called.
Fixing both of these things:
task.wait(10)
local function StartGame()
task.wait()
local GM = script.GameModes:GetChildren()
local SG = GM[math.random(1, #GM)]
print(SG)
end
StartGame()
task.wait(10)
local function StartGame()
task.wait()
local GM = script.GameModes:GetChildren()
local SG = GM[math.random(1, #GM)]
print(SG)
end
StartGame()
Involving @adark’s code snippet which is accurate. Attributed him.
I intend to use the SG to connect to the module script. I ran your script and yes it gave the module script back, but how do I run said module script? I placed a print statement in the module script to test if it would run, but it did not.
Yes I am aware I have to use the require. The module script consists of only a single print statement nothing else.
How do I get SG to require the selected game mode Module Script?
You pass require the ModuleScript Instance directly:
task.wait(10)
local function StartGame()
task.wait()
local GM = script.GameModes:GetChildren()
local SelectGamemode = GM[math.random(1, #GM)]
local SG = require(SelectGamemode)
end
Keep in mind, however, that ModuleScripts will only be executed once, and whatever they return will be cached and returned again each time you require it after the first.
Ah yes! Just directly do require instead of retyping everything. Thanks, I really have to get into the habit of not unnecessarily repeating myself when scripting.