Hi,
I don’t know how to script multiple zombies spawning on touch, then removing them once the player has died. Help would be appriciated!
Thanks.
Use a .Touched
event to detect when a part is touched - have your zombies in ReplicatedStorage and call a function when the .Touched
event is fired which clones the zombies in ReplicatedStorage and parents it to workspace (make sure you position them where you want to when you clone them).
For the removing part, you could detect if the player has died on the client through a local script by using Humanoid.Died
and firing a remote event to the server that looks for the zombies in workspace and then destroys them if found.
im confused could you explain a bit more please?
Could you tell me which parts were confusing? I’ll expand on what you didn’t understand.
The “.touched” bit was confusing
I think it would be better if the zombies were in server storage and cloned from there to workspace
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Part = workspace.Part -- whatever part you want to trigger the event
local ZombieSpawn = workspace.ZombieSpawn -- whatever part you want to have as your zombie spawn (make sure cancollide is false)
local Zombies = ReplicatedStorage.Zombies -- whatever your zombies are called in replicated storage (make sure its a model of the zombies together)
Part.Touched:Connect(function(hit) -- if the part is touched
if hit.Parent:FindFirstChild("Humanoid") then -- check if its a player
local SpawnZombies = Zombies:Clone() -- clone the zombies
SpawnZombies.Parent = workspace -- place it in workspace
SpawnZombies.Position = ZombieSpawn.Position -- position them where your zombie spawn is
end
end)
Instead of thinking of it as spawning, think of it logically and in steps.
- Detect when the part is touched using
.Touched
- Connect that event to a function to be able to run steps and procedures to do what we want
- If whatever touched the part is a player, we proceed
- We want to make a clone of the zombies instead of just moving them from our storage, that way we can keep re-using them
- After making a clone, we want to place it in the workspace where our game world exists
- Then, we want to position the zombies exactly where we want to so they can become a part of the game
In this context, yeah! Thanks!
i tested it, the script works
the only issue is
it spawns a lot of zombies at once and crashed my roblox studio
any work around like a wait function?
Make a debounce/cooldown, or delete the part after spawning the zombies (if you want it to work only once)
the script doesnt work if i want to teleport the players as you can see, it teleports the players, then i walk on the touch part but it doesnt spawn the zombies.
the script:
> local ReplicatedStorage = game:GetService("ReplicatedStorage")
> local Lighting = game:GetService("Lighting")
> local Part = Lighting.Maps.RainyCombat.TexturePart -- whatever part you want to trigger the event
> local ZombieSpawn = workspace.RCSpawner -- whatever part you want to have as your zombie spawn (make sure cancollide is false)
> local Zombies = ReplicatedStorage.RCZombies -- whatever your zombies are called in replicated storage (make sure its a model of the zombies together)
>
> Part.Touched:Connect(function(hit) -- if the part is touched
> if hit.Parent:FindFirstChild("Humanoid") then -- check if its a player
> local SpawnZombies = Zombies:Clone() math.random(1,6) -- randomises the amount of zombies spawning in
> SpawnZombies.Parent = workspace -- parents them to workspace
> print("RC's zombies spawned in") -- outputs if they spawned in
> SpawnZombies.Position = ZombieSpawn.Position
>
>
>
>
> end
> end)
i added some extra bits, by the way.
do i have to leave those parts in the workspace
thanks btw!
--> Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--> Objects
local Part = workspace.Part
local ZombieSpawn = workspace.Spawn
local Zombies = ReplicatedStorage.Zombies:GetChildren()
--> Variables
local db = false
--> Code
Part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and db == false then
db = true
local howMany = math.random(1, #Zombies) -- generate a random number between 1 and the total amount of zombies
for i = 1, howMany do -- use that number and loop through the zombies howMany number of times to spawn that amount of zombies
local spawnZombies = Zombies[i]:Clone() -- for this you can even just have Zombies[1]:Clone() instead of Zombies[i]:Clone(), they're all the same zombies anyway
spawnZombies.Parent = workspace
spawnZombies:MoveTo(ZombieSpawn.Position) -- we're using moveTo because we're positioning a model. if you use the other method we used earlier the zombies will spawn in a weird place or their behavior will become weird
wait(2)
end
end
db = false
end)
thanks very much for your help
i realised its a script not a local script
also how would u make it spawn only one zombie?