Hi! I’m running into a problem where I have multiple enemy NPC’s in my game that are supposed to attack you and players are able to fight and kill them. Since it is my first time using Collection Service, I have no clue as to why whenever I kill an enemy NPC, it doesn’t respawn sometimes and it doesn’t attack me if it did respawn.
The code works by getting the tagged models called “EnemyNPC” and then having to apply the code I wrote for them to function as is. It has an Attack function which works by having the Enemy NPC touching a player character, then firing a Attack/Hit function to that player character. When a player kills that specific Enemy NPC it will die, but also after a set amount of time, it will respawn by making a clone of it beforehand and then parenting it to the folder in the workspace. Although, after one time it respawns, it stops respawning after that and the Enemy NPC is also not attacking the player anymore.
Can you guys help me fix my code to have it where the enemy NPC’s do respawn and still attack you? Thanks.
This are the lines of code (The server script is located inside the ServerScriptService within a folder):
local CollectionService = game:GetService("CollectionService")
for _, Character in CollectionService:GetTagged("EnemyNPC") do
local Humanoid = Character:FindFirstChild("Humanoid")
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local Head = Character:FindFirstChild("Head")
local Damage = Character:GetAttribute("Damage")
local AttackSpeed = Character:GetAttribute("AttackSpeed")
local HitType = Character:GetAttribute("HitType")
local RespawnTime = Character:GetAttribute("RespawnTime")
local NPCType = Character:GetAttribute("NPCType")
local AttackAnim = Humanoid:LoadAnimation(Humanoid:WaitForChild("AttackAnim"))
local AttackSwing = HumanoidRootPart:WaitForChild("AttackSwing")
local AttackCD = false
local function Hit(hit)
if Humanoid.Health <= 0 or AttackCD then return end
if hit.Parent ~= nil then
local TargetHum = hit.Parent:FindFirstChild("Humanoid")
if hit.Parent:FindFirstChild("Humanoid") ~= nil and hit.Parent:FindFirstChild("Humanoid"):IsA("Humanoid") and TargetHum ~= Humanoid and not hit.Parent:GetAttribute("isNPC") then
AttackCD = true
local TargetHRP = TargetHum.Parent:WaitForChild("HumanoidRootPart")
local AttackHit = HumanoidRootPart:WaitForChild("AttackHit"):Clone()
AttackHit.Parent = TargetHRP
AttackHit:Play()
game.Debris:AddItem(AttackHit, AttackHit.TimeLength + 0.5)
AttackSwing:Play()
AttackAnim:Play()
TargetHum:TakeDamage(Damage)
task.wait(AttackSpeed)
AttackCD = false
end
end
end
for _, Child in pairs(Character:GetChildren()) do
if Child:IsA("Part") or Child:IsA("MeshPart") or Child:IsA("WedgePart") or Child:IsA("CornerWedgePart") then
Child.Touched:connect(Hit)
end
end
local MobClone = Character:Clone()
Character:GetAttributeChangedSignal("Died"):Connect(function()
if Character:GetAttribute("Died") == true then
print(Character.Name .. " died")
task.wait(RespawnTime)
print(Character.Name .. " respawned")
MobClone.Parent = workspace:WaitForChild("EnemyNPCS")
Character:Destroy()
print(MobClone.Name, MobClone.Parent)
end
end)
Humanoid.Died:connect(function()
Character:SetAttribute("Died", true)
task.wait(0.5)
Character:SetAttribute("Died", false)
end)
end