Random NPC Spawner problems

I’m trying to make that, A random npc spawns on the spawn part.

The spawn part is already finished, The only problem is Npc Randomizing.

Code:

local NPCSpawners = workspace.EnemySpawns:GetChildren()

local NPCS = game.ReplicatedStorage.NPCS

function SpawnRandomNPC()

local RandomSpawners = NPCSpawners[math.random(#NPCSpawners)]

local ChoosenNPC = NPCS[math.random(#NPCS)]

ChoosenNPC.Parent = workspace

ChoosenNPC.HumanoidRootPart.Position = RandomSpawners.Position

end

SpawnRandomNPC()

If you have any fixes please reply to this post.

1 Like

And what exactly is the issue that’s occurring? This is the kind of information that is needed to make any progress. Just giving code and saying their is a problem doesn’t help.

My first assumption is you aren’t calling :GetChildren() on game.ReplicatedStorage.NPCS. Which means their is no table being assigned to your NPCS variable.

The other issue I see is you are not creating a Clone of your NPCS which means you are removing the NPCS from ReplicatedStorage and putting them in Workspace. Click that link that’s posted and it should help you with using the models stored in ReplicatedStorage to be reference models.

4 Likes

I think you’re missing a #NPCS:GetChildren()

You’re indexing an array rather than a length.

EDIT: the post above mine nailed it ^^^

1 Like

Oh! I really forgot it.

This actually helped. Thanks!

2 Likes