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
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
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()
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.
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.
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
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.
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.
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?
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()
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!