"Workspace.NPCSA.RigA.UpperTorso.Script:19: invalid argument #2 to 'random' (interval is empty)" But its clearly not

I am getting the error “Workspace.NPCSA.RigA.UpperTorso.Script:19: invalid argument #2 to ‘random’ (interval is empty)” even though there are items in the folder.

I am currently creating a NPC and this is the code including errors:

local speech2 = game.ReplicatedStorage.SpeechHandler.Speech2
local AvabSpeech2 = speech2:GetChildren()
local ChosenSpeech2 = AvabSpeech2[math.random(1,#AvabSpeech2)]
local speech2 = ChosenSpeech2.Name

This is the folder which clearly has items in:
image

Is there something obvious I am missing? This same system worked with “Speech1” and its literally the same thing.

Are you adding the Events in a different script? Try printing out what ChosenSpeech2 gives out

If it’s a empty table, then the script probably doesn’t detect all the Children inside the Folder it the moment it runs

local speech2 = game.ReplicatedStorage.SpeechHandler.Speech2
local AvabSpeech2 = speech2:GetChildren()

print(AvabSpeech2)

local ChosenSpeech2 = AvabSpeech2[math.random(1,#AvabSpeech2)]
local speech2 = ChosenSpeech2.Name

It is giving me {} which means it isn’t finding them, how can you resolve this issue?

Could we see the full hierarchy from where the Events & Folders are supposed to be before you start the simulation?

Example: game > ReplicatedStorage > SpeechHandler > Speech1

Apologies for wasting your time I just realised when extending the explorer I had duplicated it by mistake and the script was trying to find the items in the empty folder!

A h

That would explain why it was printing out the empty table then

Hopefully if you change that, then everything should work fine!

1 Like