i have a problem with my enemy where when it runs the respawn script it just respawns it but its dead, i’ve tried various different things but i can’t seem to figure it out, some guidance would be greatly apprenticed.
here is my script
local EnemiesFolder = workspace.Enemies
local EnemyTable = {}
local Enemies = EnemiesFolder:GetChildren()
--Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
for _,v in pairs(Enemies) do
table.insert(EnemyTable, v)
--Calling Module
local EnemyConfig = require(v.EnemyConfig)
--Variables
local Enemy = v
local EnemyHumanoid = Enemy.Humanoid
local EnemyHumanoidRootPart = Enemy.HumanoidRootPart
local EnemySpawnSpot = Enemy.SpawnSpot
local AttackCoolDown = false
--ModuleVariables
local Health = EnemyConfig.Health
local WalkSpeed = EnemyConfig.WalkSpeed
local JumpPower = EnemyConfig.JumpPower
local Damage = EnemyConfig.Damage
local FollowDistance = EnemyConfig.FollowDistance
local RespawnTime = EnemyConfig.RespawnTime
local Range = EnemyConfig.Range
local Gold = EnemyConfig.Rewards.Gold
local EXP = EnemyConfig.Rewards.EXP
--Animations
local Animator = Instance.new("Animator")
Animator.Parent = EnemyHumanoid
local AttackAnimationID = EnemyConfig.AttackAnimationID
local AttackAnimation = Instance.new("Animation")
AttackAnimation.Parent = Enemy.Humanoid
AttackAnimation.Name = "AttackAnimation"
AttackAnimation.AnimationId = AttackAnimationID
local AttackAnimationTrack = EnemyHumanoid.Animator:LoadAnimation(AttackAnimation)
local WalkAnimationID = EnemyConfig.WalkAnimationID
local WalkAnimation = Instance.new("Animation")
WalkAnimation.Parent = Enemy.Humanoid
WalkAnimation.Name = "WalkAnimation"
WalkAnimation.AnimationId = WalkAnimationID
local WalkAnimationTrack = EnemyHumanoid.Animator:LoadAnimation(WalkAnimation)
local IdleAnimationID = EnemyConfig.IdleAnimationID
local IdleAnimation = Instance.new("Animation")
IdleAnimation.Parent = Enemy.Humanoid
IdleAnimation.Name = "IdleAnimation"
IdleAnimation.AnimationId = IdleAnimationID
local IdleAnimationTrack = EnemyHumanoid.Animator:LoadAnimation(IdleAnimation)
--Sounds
local AttackSoundID = EnemyConfig.AttackSoundID
local AttackSound = Instance.new("Sound")
AttackSound.Name = "AttackSound"
AttackSound.Parent = Enemy
AttackSound.SoundId = AttackSoundID
--AssigningStats
EnemyHumanoid.MaxHealth = Health
EnemyHumanoid.Health = Health
EnemyHumanoid.WalkSpeed = WalkSpeed
EnemyHumanoid.JumpPower = JumpPower
EnemyHumanoidRootPart:SetNetworkOwner(nil)
local PlayerTable = {}
local AtSpawnPoint = false
--PlayWalkingAnimation
local function PlayWalkAnimation()
local Humanoid_State = EnemyHumanoid:GetState()
if Humanoid_State == Enum.HumanoidStateType.Running then
if WalkAnimationTrack.IsPlaying then
return
end
WalkAnimationTrack:Play()
elseif Humanoid_State == Enum.HumanoidStateType.None then
local RunningAnimations = EnemyHumanoid:GetPlayingAnimationTracks()
for i, Animation in pairs(RunningAnimations) do
if Animation.IsPlaying == true then
Animation:Stop()
end
end
end
end
--Moving Enemy Function
local function MoveEnemy()
local Target = nil
local PlayersInGame = Players:GetPlayers()
for _,Player in pairs(PlayersInGame) do
local Character = Player.Character
if Character then
local CharacterHumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
if CharacterHumanoidRootPart and (EnemyHumanoidRootPart.Position - CharacterHumanoidRootPart.Position).Magnitude < Range then
if Target then
if (EnemyHumanoidRootPart.Position - Target.Position).Magnitude > (EnemyHumanoidRootPart.Position - CharacterHumanoidRootPart.Position).Magnitude then
Target = CharacterHumanoidRootPart
end
else
Target = CharacterHumanoidRootPart
PlayWalkAnimation()
IdleAnimationTrack:Stop()
--Put Players Into Reward Table
if table.find(PlayerTable, Target.Parent) then
else
table.insert(PlayerTable, Target.Parent)
end
end
end
end
--Stop Moving
if Target == nil then
if IdleAnimationTrack.IsPlaying then
else
IdleAnimationTrack:Play()
WalkAnimationTrack:Stop()
wait(5)
--TP Back
if WalkAnimationTrack.IsPlaying then
else
if Target == nil then
if AtSpawnPoint == true and Target == nil then
AtSpawnPoint = false
EnemyHumanoidRootPart.CFrame = EnemySpawnSpot.CFrame
end
end
end
end
end
end
if Target then
EnemyHumanoid:MoveTo(Target.Position)
end
local Humanoid_State = EnemyHumanoid:GetState()
end
--Attack Player
Enemy.HitBox.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
if Hit.Parent:HasTag("Enemy") then
else
if AttackCoolDown == false then
AttackCoolDown = true
AttackSound:Play()
AttackAnimationTrack:Play()
Enemy.HitBox.CanTouch = false
local Humanoid = Hit.Parent.Humanoid
Humanoid.Health = Humanoid.Health - Damage
end
end
end
if AttackCoolDown == true then
task.wait(2)
AttackCoolDown = false
Enemy.HitBox.CanTouch = true
end
end)
Enemy.SpawnSpot.Touched:Connect(function(Hit)
if Hit.Parent == Enemy then
EnemyHumanoid:ChangeState(Enum.HumanoidStateType.PlatformStanding)
WalkAnimationTrack:Stop()
AtSpawnPoint = true
if IdleAnimationTrack.IsPlaying then
else
IdleAnimationTrack:Play()
end
end
end)
--When Health = 0
EnemiesFolder.ChildAdded:Connect(function()
EnemyHumanoid.died:Connect(function()
task.wait(2)
local EnemyClone = Enemy:Clone()
EnemyClone.Parent = EnemiesFolder
Enemy:Destroy()
end)
end)
RunService.Stepped:Connect(MoveEnemy)
end