Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Needing Help with Spawning my Enemies Randomly Across my Map?

Asked by 3 years ago
Edited 3 years ago

So I'm trying to make Zombies spawn randomly across my map but I'm having trouble with it. I'm following this guide (https://education.roblox.com/en-us/resources/arcade-game-spawning-enemies) and it works for Parts but not a Model. Basically it'll spawn the 100 Enemy Balls on my map but when I change it to fit my Zombies it only spawns 1 of them and when I kill it it doesn't respawn. Here is the Zombie I am using (https://www.roblox.com/library/3924238625/Drooling-Zombie-Rthro) and here is my code

-- Randomly spawns the number of enemies set in ENEMY_COUNT

local ENEMY_COUNT = 100
local SAFE_ZONE_SIZE = 50
local RESPAWN_DURATION = 3

local ServerStorage = game:GetService("ServerStorage")

local function spawnEnemies(count)
    local baseplateSize = workspace.Baseplate.Size

    -- Loops until there are the amount of enemies set in ENEMY_COUNT
    for i = 1, count do
        -- Makes a copy of EnemyBall
        --local enemy = ServerStorage.Enemies.EnemyBall:Clone()
        local enemy = ServerStorage.Enemies.Zombie:Clone()
        enemy.Parent = workspace.Enemies

        -- Places EnemyBall randomly on the baseplate
        local randomX = math.random(baseplateSize.X / -2 - SAFE_ZONE_SIZE, baseplateSize.X / 2 + SAFE_ZONE_SIZE)
        local randomZ = math.random(baseplateSize.Z / -2 - SAFE_ZONE_SIZE, baseplateSize.Z / 2 + SAFE_ZONE_SIZE)
        enemy.Position = Vector3.new(randomX , 20, randomZ)

    end
end

-- Have enemies spawn on the baseplate
spawnEnemies(ENEMY_COUNT)

Also a side question, how would I go about spawning from ServerStorage.Enemies folder so I could add different looking zombies? Any help would be greatly appreciated. Thanks in advance!

2 answers

Log in to vote
0
Answered by
Desmondo1 121
3 years ago

I don't have an answer to your original question. But if you want a random selection of zombies. Then you can do this

(By the way, change "Zombie1", to the name of whatever zombie you have, and "Zombie2", as the name of the second zombie you want, and then "Zombie3", as the name as the third zombie's name if you have a third zombie)

local Zombies = {ServerStorage.Zombie1,ServerStorage.Zombie2,ServerStorage.Zombie3}

for i=1, 30 do
local RandomNumber = math.random(1,#Zombies)

local ChosenZombie = Zombies[RandomNumber]

spawnEnemies(ENEMY_COUNT)

wait(10)
end

Before I explain, let me just say a couple things.

First, you can modify the amount of zombies in the table at the top.

Secondly, you can change the amount of times a zombie spawns and the wait time before another spawns

Thirdly, you would need to modify your function to spawn the ChosenZombie and not just the same zombie.

Now for explaining.

At the top is a table, if you don't understand tables I recommend looking at the wiki.

Then we have a for loop which you understand because you used it in your code.

And then a random number generator, I prefer doing this over this:

local ChosenZombie = Zombies[math.random(1, #Zombies)]

Because it makes your code look cleaner and easier to understand.

Then the # is just tells the code "Take a random thing from the table, from the first, to the last".

Then you just call upon the function that you made, but you'll need to modify that function so it spawns the ChosenZombie.

I know you can pass parameters through remote events. But I don't know if you can pass them through functions, just in case, try doing this

local Zombies = {ServerStorage.Zombie1,ServerStorage.Zombie2,ServerStorage.Zombie3}

for i=1, 30 do
local RandomNumber = math.random(1,#Zombies)

local ChosenZombie = Zombies[RandomNumber]

spawnEnemies(ENEMY_COUNT, ChosenZombie)

wait(10)
end

And in your function do this:

local function spawnEnemies(count, ChosenZombie)
        local baseplateSize = workspace.Baseplate.Size


        for i = 1, count do


            local enemy = ChosenZombie
            enemy.Parent = workspace.Enemies


            local randomX = math.random(baseplateSize.X / -2 - SAFE_ZONE_SIZE, baseplateSize.X / 2 + SAFE_ZONE_SIZE)
            local randomZ = math.random(baseplateSize.Z / -2 - SAFE_ZONE_SIZE, baseplateSize.Z / 2 + SAFE_ZONE_SIZE)
            enemy.Position = Vector3.new(randomX , 20, randomZ)

        end
    end

Ad
Log in to vote
0
Answered by 3 years ago

That worked for selecting a random Zombie from

local Zombies = {ServerStorage.Enemies.Zombie1,ServerStorage.Enemies.Zombie2,ServerStorage.Enemies.Zombie3}

and spawning it in but I still need help with them Respawning after they're killed & Spawning in 100 Zombies as defined by

local ENEMY_COUNT = 100

Answer this question