Hello, I want to make Zombie NPC spawn on random parts, “ZombieSpawn”. However it only spawns on the first “ZombieSpawn” part.
I am very new to scripting and would like to pursue Comp Sci, how do i go about to solve this?
I have tried to google this problem for many days but couldn’t find solution.
function SpawnZombie(Zombie_Type, CF) -- spawn zombie command; call when wanting to spawn zombie. pass zombie type and desired CFrame
local Zombie_Info = Zombie_Data[Zombie_Type]
if Zombie_Info then
local Clone= Zombie_Models[Zombie_Type]:Clone()
Clone.Parent = workspace.MobsFolder
local Zombie = Zombie_Class.new(Model, Zombie_Info)
--Model:SetPrimaryPartCFrame(CF + Vector3.new(0,Model.Humanoid.HipHeight + Model.HumanoidRootPart.Size.Y/2,0))
Model.HumanoidRootPart.CFrame = (CF + Vector3.new(0,Model.Humanoid.HipHeight + Model.HumanoidRootPart.Size.Y/2,0))
Model.PrimaryPart.Anchored = false
Zombies[Zombie.humanoid] = Zombie
end
end
for x, spawner in pairs(mobSpawns:GetChildren()) do
while mobsToSpawn > 0 do
mobsToSpawn = mobsToSpawn - 1
SpawnZombie(mobTypeSpawn, spawner.CFrame + Vector3.new(math.random(1,10),10,0))
end
end
You’d have to put the spawners as a separate value and put the while loop outside of that loop and just get a random spawner. something like this basically
function SpawnZombie(Zombie_Type, CF) -- spawn zombie command; call when wanting to spawn zombie. pass zombie type and desired CFrame
local Zombie_Info = Zombie_Data[Zombie_Type]
if Zombie_Info then
local Clone= Zombie_Models[Zombie_Type]:Clone()
Clone.Parent = workspace.MobsFolder
local Zombie = Zombie_Class.new(Model, Zombie_Info)
--Model:SetPrimaryPartCFrame(CF + Vector3.new(0,Model.Humanoid.HipHeight + Model.HumanoidRootPart.Size.Y/2,0))
Model.HumanoidRootPart.CFrame = (CF + Vector3.new(0,Model.Humanoid.HipHeight + Model.HumanoidRootPart.Size.Y/2,0))
Model.PrimaryPart.Anchored = false
Zombies[Zombie.humanoid] = Zombie
end
end
local spawners = mobSpawns:GetChildren()
for i = mobsToSpawn, 0, -1 do
SpawnZombie(mobTypeSpawn, spawners[math.random(#spawners)].CFrame + Vector3.new(math.random(1,10),10,0))
end
Store all the spawns in a variable and loop for how many mobs you have to spawn and put them in a random position near a spawn. It’s also recomemnded that you use a for loop for how you were doing it. But if needed, you can change it back to the while loop
The script works! Thank you.
I would like to understand it a bit further,
What does the for i = mobsToSpawn, 0, -1 mean?
and what does spawners[math.random(#spawners)] mean too?
As per my knowledge, i thought for loops are used with in pairs.
I am unsure of what keywords to put on google to help me to understand. Hope you could help too. Thank you!
Also for my code +Vector3.new, i wanted it to spawn on a random X coordinate between 1,10 of the ZombieSpawn however it isn’t working.
Basically for i = mobsToSpawn, 0, -1 is an numerical loop, the first argument is the number to start with, the 2nd being the number to end at, and the 3rd one is optional but it is how the number decrements/increments every loop. By default it is set to 1, meaning the number increases by 1 till it reaches the ending number. Basically, it does this
Do this loop starting from how many mobs we have to spawn and ending at 0, going down at increments of -1
spawners[math.random(#spawners)] is basically retrieving a random thing from the spawners variable. When do you GetChildren(), it returns an array of all the children of an instance, the code basically generates a random number from 1 to how many things are in the array, as the # is used to get the length of something, and then uses that number to get a random thing from the array
Anytime! If you have anymore issues don’t be afraid to make another post!
And to try and explain that line again, imagine this. You have 3 things inside of a part and you use GetChildren(), that will return a table containing all the things inside of that part. When you do math.random(#spawners), if there are 3 spawners, it will generate a random number between 1 and 3, and then uses that number to get a certain spawner based on its position in the table.