Trying to spawn a random enemy on a part

I am working on a simple script that takes an enemy from a folder and places it in the world at creation. Here is how I have it set up.

  • I have a folder in my Workspace names EnemySpawns with parts in it. Those parts are scatered around the map and simply named Part.
  • I have a folder in my ReplicatedStorage named Enemies and inside that folder is, as expected a bunch of enemies.

So with the script I am simply trying to take a random enemy from that folder and place it on a random spawn point in the world when the map is loaded. Just so randomize all my enemies so there not in the same location every time. The script doesn’t give any errors or warnings but also doesn’t seem to work at all. Nothing happens. I am new to this and trying to learn so I am sorry if it’s something stupid. Thanks for any help! Here is the script:

local Spawns = workspace.EnemySpawns:GetChildren()
local NPCS = game.ReplicatedStorage.Enemies

function SpawnRandomNPC()

local RandomSpawners = Spawns[math.random(#Spawns)]
local ChoosenNPC = NPCS[math.random(#NPCS:GetChildren())]

ChoosenNPC.Parent = workspace
ChoosenNPC.HumanoidRootPart.Position = RandomSpawners.Position

end

SpawnRandomNPC()

Try this perhaps?

local Spawns = workspace.EnemySpawns
local NPCS = game.ReplicatedStorage.Enemies

function SpawnRandomNPC()
	local RandomSpawners = Spawns[math.random(#Spawns:GetChildren())]
	local ChoosenNPC = NPCS[math.random(#NPCS:GetChildren)]:Clone()
	
	print(RandomSpawners.Name)
	print(ChoosenNPC.Name)

	ChoosenNPC.Parent = workspace
	ChoosenNPC.HumanoidRootPart.CFrame = RandomSpawners.CFrame * CFrame.new(0,3.25,0)
end

SpawnRandomNPC()

I added those prints to Debug if something is wrong with what you’re receiving

1 Like
local ChoosenNPC = NPCS:GetChildren()[math.random(#NPCS:GetChildren())]

you didn’t get the table of NPCS, this should fix it

1 Like

Thanks a lot you two, I tried both suggestions with no Luck. I took Embats suggested and added a print to both suggested corrections. Embats didn’t produce an error or print or anything. TheKgroup I did yours and actually got some notices in the console output that are:

14:52:53.732 Script ‘Workspace.RandomEnemies’, Line 7 - function SpawnRandomNPC - Studio - RandomEnemies:7
14:52:53.733 Script ‘Workspace.RandomEnemies’, Line 17 - Studio - RandomEnemies:17

The actual script that resulted in at least the console is:

local Spawns = workspace.EnemySpawns:GetChildren()
local NPCS = game.ReplicatedStorage.Enemies

function SpawnRandomNPC()

local RandomSpawners = Spawns[math.random(#Spawns)]
local ChoosenNPC = NPCS:GetChildren()[math.random(#NPCS)]

print(RandomSpawners.Name)
print(ChoosenNPC.Name)

ChoosenNPC.Parent = workspace
ChoosenNPC.HumanoidRootPart.Position = RandomSpawners.Position

end

SpawnRandomNPC()

This is a “script” i have in workspace all by itself. I don’t think I need it attached to anything or in a specific location but this is admittedly my first time working with trying to move something from replicated storage, so maybe I am wrong? thanks for helping!

D’oh! I completely forgot about the fact that I forgot to make them get the children! Try this perhaps?

local Spawns = workspace.EnemySpawns:GetChildren()
local NPCS = game.ReplicatedStorage.Enemies:GetChildren()

function SpawnRandomNPC()

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

	print(RandomSpawners.Name)
	print(ChoosenNPC.Name)

	ChoosenNPC.Parent = workspace
	ChoosenNPC.HumanoidRootPart.Position = RandomSpawners.Position
end

SpawnRandomNPC()
1 Like

PROGRESS! lol

SO the console is now calling for a random enemy from the folder, I have tested a few times and get a different enemy being called each time.

15:29:08.422 Spawn - Server - RandomEnemies:9
15:29:08.422 Zombie - Server - RandomEnemies:10

&

15:30:57.553 Spawn - Server - RandomEnemies:9
15:30:57.554 CriminalPiggy - Server - RandomEnemies:10

I am even getting notifications in the console that the enemy is added. Such as interaction warnings, etc. However, the enemies don’t show up in the world. Or they are invisible? While testing I look at the workspace to see if it ever gets added to the workspace and it doesn’t. I assume that’s why I am not seeing it. Thie perplexes me because there is no warnings or errors and the script calls for the parent of the NPC to be the workspace. Any suggestions? Thanks again for the patience and helping me. Means a lot.

Maybe change this to this?

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

The other code should work, I think it’s maybe I forgot to clone the NPC

1 Like

Figured out the issue. Just unsure how to fix it. After a bunch of testing I realized the Enemy was being cloned as intended but was “flying” around the map super fast and disapearing quickly, removing it from the workspace. If I comment out the positioning line it places the enemy exactly where it was when I moved it to replicated storage and works perfectly but this obviously negates the “random” effect I want. I tried removing the humanoid part and that didn’t work.

ChoosenNPC.Position = RandomSpawners.Position

Is there a better method for positioning the entire enemy where it should be. This issue seems to be for all enemies and I confirmed they all do have a humanoidrootpart. Unsure why it would be freaking out.

Try this perhaps?

ChoosenNPC.HumanoidRootPart.Position = RandomSpawners.Position + Vector3.new(0,3.25,0)

Edit: Not sure if I’ll be able to assist for any longer as it is late for me, if it doesn’t fully help, I think someone else could come and help or if no one comes, you could wait for me to wake up if necessary

1 Like

Same result. So freaking weird. I shared a picture to help explain. The yellow circle is where the “spawner” is and the enemy should be positioned but the huge red box is where the enemy is spawning and flying around way over there. Not the same location each time either, the enemy just spawns in random locations flinging everywhere dead until it despawns.

Have a good night mate. Thanks for the help you have got me this far. Really appreciate it!

your problem is very simple to solve, you just need to add a 1 as the first parameter to the math.random()

local RandomSpawners = Spawns[math.random(1,#Spawns)]
local ChoosenNPC = NPCS[math.random(1,#NPCS:GetChildren())]

ChoosenNPC.Parent = workspace
ChoosenNPC.HumanoidRootPart.Position = RandomSpawners.Position

Thanks for trying. I am not sure what the 1 adds but I tried the suggestion you made with the same results. The enemy just spawns and starts flinging around the sky until it dies. So weird. :frowning:

I think it has to be something with the Spawner itself, just to confirm, it is cancollide off, anchored, and invisible? Maybe you need to position the HumanoidRootPart before parenting the NPC to workspace?

1 Like

So in all my testing for the few hours yesterday, I ended up making it a super simple script that I just attached to a part (EnemySpawn) I can place these spawns around the map where I want an enemy and it will grab a random enemy.

Anyways, with your final suggestion, it works perfectly. I have no idea why bringing it to the workspace AFTER positioning it was such a big issue but it works perfect now. Thank you so so much!

The final working version is a simple 4 line code that gave me such a headache. lol

local NPCS = game.ReplicatedStorage.Enemies:GetChildren()
local ChosenNPC = NPCS[math.random(#NPCS)]:Clone()

ChosenNPC.PrimaryPart.Position = workspace.EnemySpawns.Spawn1.Position
ChosenNPC.Parent = workspace

1 Like

I’m glad it finally works for you! If you have anymore issues don’t be afraid to make another post! Also yea I’m not sure either why positioning it first and then parenting it works, but hey if it works it works haha!

1 Like