Hello. I’m trying to have a function that spawns in an enemy after its “parent enemy” dies. The mob is summoned via a modulescript and is given quite a bit of parameters when spawning. The function works fine, but once the “parent enemy” is destroyed, the “child enemy” stops moving, presumably because the function that it was called on no longer exists. Is there any way to keep this mob module function running even after what called it is gone?
local tower = script.Parent
dead = false
local mob = require(game.ServerScriptService.Main.Mob)
local function summonEnemy()
local map = workspace.Map:FindFirstChildOfClass("Folder")
local mobtospawn = game.ServerStorage.Mobs["MuffinSheep"]
local quantity = 1
local waittime = 0.1
local waypoint = script.Parent.MovingTo.Value
local path = script.Parent.WaypointsType.Value
local height = (tower.PrimaryPart.Size.Y/2) + tower.Humanoid.HipHeight
local offset = Vector3.new(0, height, 0)
local mobheight = (mobtospawn.PrimaryPart.Size.Y/2) + mobtospawn.Humanoid.HipHeight
local moboffset = Vector3.new(0, mobheight, 0)
local SizeDifference = moboffset - offset
local position = script.Parent.HumanoidRootPart.CFrame + SizeDifference
mob.Spawn(mobtospawn.Name,quantity,waittime,map,waypoint,path,position)
end
tower.Humanoid.Changed:Connect(function()
if tower.Humanoid.Health <= 0 and dead == false then
dead = true
wait(2.9)
summonEnemy()
end
end)
I want to make it noteworthy before anyone asks, the wait(2.9) at the bottom is not preventing the script from working. The enemy is delayed from getting destroyed before the wait is over, and once it is over, the enemy then gets destroyed.
tower.Humanoid.Changed:Connect(function()
if tower.Humanoid.Health <= 0 and dead == false then
dead = true
wait(2.9)
summonEnemy()
dead = false
end
end)
After summoning an enemy you need to set the dead value to false
Didn’t seem to work. The dead = true prevents that part of the script from running multiple times, besides, as i said the script gets deleted after the enemy is spawned.
I could try and make the script call upon a remoteevent and then have another script that won’t get deleted with the code. and then just move the code there. I think that would be a better solution now that I think about it.
If you want scripts to affect the game environment after the object(s) of which they pertain to have been removed (destroyed) then you should avoid placing those scripts inside of said object(s).