I’m working on a game similar to Randomly Generated Droids where a random room spawns with random bots. This script is in the model of the room, meant to spawn a random bot on a part named ‘NPCSpawner’ and delete it. The NPC is meant to spawn with a modifier, so I have set the minimum requirement for the variable m to 1, but when the script runs it returns
Workspace.generatedrooms.2.spawnerscript:23: invalid argument #1 to ‘random’ (number expected, got nil)
Here’s the script:
local NPCS = game.Workspace.botfolder:GetChildren()
function SpawnRandomNPC()
for _, child in pairs(script.Parent:GetChildren()) do
if child.Name == (‘NPCSpawner’) then
local ChosenNPC = NPCS[math.random(#NPCS)]:Clone()
ChosenNPC.Parent = child.Parent
ChosenNPC.Torso.CFrame = child.CFrame
local m = math.random(0,100)
local g = 0
local modifiers = game.ServerStorage.modifiers
local parent = ChosenNPC
local hum = parent.Zombie
local highlight = parent.Highlight
local name = parent.Name
print(m)
if m > 1 then
local modifierchildren = tonumber(modifiers:GetChildren())
g = math.random(modifierchildren)
local modifier = modifiers[math.random(#modifiers)]
print(modifier.Name)
print(g)
if modifier.Name == 'Slow' then
hum.WalkSpeed = hum.WalkSpeed * 0.5
highlight.FillColor.B = 138
highlight.FillColor.G = 138
highlight.FillColor.R = 138
highlight.FillTransparency = 0.9
highlight.OutlineColor.B = 138
highlight.OutlineColor.G = 138
highlight.OutlineColor.R = 138
highlight.OutlineTransparency = 0
parent.modifier.Value = 'slow'
parent.Event:FireAllClients()
end
end
child:Destroy()
end
end
end
if script.Parent.Parent.Name == ‘generatedrooms’ then
SpawnRandomNPC()
end
if m > 1 then
print(tonumber(modifiers:GetChildren()))
local modifierchildren = tonumber(modifiers:GetChildren())
To show what’s being referenced in the next line.
The problem isn’t really with that line, it’s with what values you are using. Go back through your code to figure out why the steps aren’t working the way you expect.
local modifierchildren = tonumber(modifiers:GetChildren())
In here you are trying to turn to a number something that is a table. You simply can’t do that. :GetChildren() returns a table with the children of the given Instance. Simply remove tonumber().
…
When making a post, you must pay attention to your own code to be sure your issue isn’t too simple, such as the one I’ve said above. If you’ve read about how # works and what :GetChildren() really does, this could’ve been resolved much earlier.
Also, you did not give enough information. You claim that there’s an error being thrown, but don’t easily reference what line it is being thrown for us. The code in the post isn’t correctly formated, that’s why some people in this comment section haven’t considered where the error actually comes from.
Either way, it looks like the error is coming from this line:
local modifier = modifiers[math.random(#modifiers)]
#modifiers is nil because “modifiers” is not a table, but simply the path to the Instance. Change the line local modifiers = game.ServerStorage.modifiers to local modifiers = game.ServerStorage.modifiers:GetChildren()
Remove the tonumber(). Tonumber converts a string to a number if it’s possible eg “773” can be converted but “cookie” can’t. If it’s not possible to convert or wrong param supplied, Nil is returned, which is what’s causing the error
Add # to make it access the number of children, i.e. g = math.random(#modifierchildren)
I suspect that you meant to use g in this case, so replace it with
local modifier = modifierschildren[g]
Updated Code
local NPCS = game.Workspace.botfolder:GetChildren()
function SpawnRandomNPC()
for _, child in pairs(script.Parent:GetChildren()) do
if child.Name == ('NPCSpawner') then
local ChosenNPC = NPCS[math.random(#NPCS)]:Clone()
ChosenNPC.Parent = child.Parent
ChosenNPC.Torso.CFrame = child.CFrame
local m = math.random(0,100)
local g = 0
local modifiers = game.ServerStorage.modifiers
local parent = ChosenNPC
local hum = parent.Zombie
local highlight = parent.Highlight
local name = parent.Name
print(m)
if m > 1 then
local modifierchildren = modifiers:GetChildren()
g = math.random(#modifierchildren)
local modifier = modifierschildren[g]
print(modifier.Name)
print(g)
if modifier.Name == 'Slow' then
hum.WalkSpeed = hum.WalkSpeed * 0.5
highlight.FillColor.B = 138
highlight.FillColor.G = 138
highlight.FillColor.R = 138
highlight.FillTransparency = 0.9
highlight.OutlineColor.B = 138
highlight.OutlineColor.G = 138
highlight.OutlineColor.R = 138
highlight.OutlineTransparency = 0
parent.modifier.Value = 'slow'
parent.Event:FireAllClients()
end
end
child:Destroy()
end
end
end
if script.Parent.Parent.Name == 'generatedrooms' then
SpawnRandomNPC()
end