Make zombies spawn on random locations?

How can I go about doing this without the zombies getting clipped inside of buildings?
I know this looks like a duplicate, but the answers didn’t give anything useful.

Basically I want something like whenever you scale a spawnlocation to a whole map, players spawn randomly on that spawn’s top, except it’s zombies.

1 Like

You should probably mark specific locations for your zombie’s to spawn in the map. With a table like so:

local ZSpawn = {CFrame.new(X,Y,Z),CFrame.new(X,Y,Z),...}

That is probably a lot more efficient then parts, I suppose that’ll do until I get the direct answer to my question

1 Like

Well direct direct answers, is probably making the whole script* for you bro.

But a more complete answer would be this:

local ZSpawn = {CFrame.new(X,Y,Z),CFrame.new(X,Y,Z),...}
local Zombie = --Your Zombie
local Choice = math.random(1,#ZSpawn)

while SpawningValue == true do
   wait(--Time between your zombies spawning)
   Zombie.CFrame = ZSpawn[Choice]
end

Fair point, but what I mean is making the zombies spawn on top of buildings instead of clipping inside of them. I’ve got basically everything down, random locations, and the spawning, but I don’t know how to achieve this.

Without a reason it’s probably really confusing on why I would exactly want this, but it’s simple; the players can just camp the zombie spawn points and get basically infinite stats, unless you have some special zombie to counter that.

Yes very fair point, but remember that the table can take a lot of different value’s. So why not make some these spawnpoint’s be inaccesable to players. Surely they can’t camp every single one or the mayority right?

Edit the Y value in the CFrame.new() to be one top of the buildings. or even:

local ZSpawn = {Building.CFrame + Vector3.new(0,10,0),...}--for example
local ZSpawn = {Building1.CFrame,Building2.CFrame,...}
--Other functions after ZSpawn
Zombie.CFrame = ZSpawn[Choice] + Vector3.new(0,10,0) -- for example

Or something like it.

1 Like

In my opinion if you want to be more specific about the spawning location of your zombies I would just put all of the spawn locations in a folder and use that instead.

I’m not too sure why you wouldn’t want to anyway:

local locations = [...]:GetChildren()
local Zombie = [..]:WaitForChild("...")

math.randomseed(os.time); -- new seed generated every time
local location = locations[math.random(1, #locations)]
Zombie.CFrame = location.CFrame


2 Likes

OP mentioned that he didn’t want his zombies spawning inside buildings, much less clipping in their walls. Which is why table’s came to mind.

Though using a folder is also a neat, if not better idea. and OP would need to use a for loop instead, to get every child.

Though I have to ask, what’s the math.randomseed() for?
And “;” isn’t necesarry in Lua

By using the spawn location objects, you can just manually move them to where you want them to be in Studio so that shouldn’t be a problem.

Though using a folder is also a neat, if not better idea. and OP would need to use a for loop instead, to get every child.

GetChildren is a method of an object that returns a table of every child of that object. It would be inefficient to loop through in that situation instead of just directly indexing.

And “;” isn’t necesarry in Lua

I’m a C++ dude so I sometimes include semicolons out of a habit, but it was just to make it clear that it was an unordinary statement in this situation.

Though I have to ask, what’s the math.randomseed() for?

Instead of explaining it myself, here’s a quote from the LUA docs:

You can set a seed for the pseudo-random generator with the randomseed function; its only numeric argument is the seed. Usually, when a program starts, it initializes the generator with a fixed seed. That means that, every time you run your program, it generates the same sequence of pseudo-random numbers.

tl;dr, just makes sure that you dont get the same sequence of indexes over and over again

3 Likes

1:
Get multiple spawn location objects around the map where you want them to spawn.

2:
Get the position of the objects.

3:
Use math.random() to change the Position.

Example code:


local parts = {workspace.Spawn1,workspace.Spawn2,workspace.Spawn3}

function getPos(p)
	local x,z = math.random(p.Position.X,p.Position.X+p.Size.X)-(p.Size.X/2),math.random(p.Position.Z,p.Position.Z+p.Size.Z)-(p.Size.Z/2)
	return Vector3.new(x,0,z)
end

for _, v in pairs(parts) do
	local zombie
	local p = getPos(v)
	zombie.Position = p
end