NPC health broken after cloning

hi all,
im working on npcs that respawn for my game, and i’m having a strange problem. for some reason, whenever an npc dies and respawns, it suddenly dies from any damage, no matter how much health it should have.

the first orc that dies is the first one to spawn when the game loads. the second one just keels over, even though he should take 4 hits.

it doesn’t throw any errors or anything like that, so it’s been hard to pin down. at first I thought it was a problem with the weapon, (since it’s custom) but it all works as it should on anything that hasn’t died already.
then I thought it was a problem with the respawn, and that it was accidentally cloning the dead body that had no health. so I tried setting the orc’s health to max when it spawns, saving a new instance of the clone from server storage every time the thing respawns, and even shifting it all around so that an invisible part respawns them, and they just handle the dying animation. no luck.

anyway, here’s the code:
block that spawns them:

function respawn()
	if currentClone ~= nil then
		currentClone.Parent = game:GetService("Workspace")
	end
	currentClone = nil
	currentID = nil
	wait(respawnTime)
	currentClone = mobToClone:Clone()
	currentID = RNGGen.NumGen(0,999999999999)
	currentClone:SetAttribute("uniqueID",currentID)
	currentClone.PrimaryPart.CFrame = script.Parent.CFrame
	currentClone.Parent = script.Parent
end
respawnEvent.Event:Connect(function(uniqueID)
	if uniqueID == currentID then
		respawn()
	end
end)
respawn()

then the script on the orc:

Humanoid.Died:connect(function()
	script.Parent.HumanoidRootPart.Anchored = true
	respawnAnim:Play()
	respawnAnim.Stopped:wait()
	bodyEvent:Fire(1,CFrame.new(torso.CFrame.Position)) --fires body handler with data
	script.Parent.Name = "Corpse"
	loopAnim:Play()
	respawnEvent:Fire(script.Parent:GetAttribute("uniqueID"))
	wait(300)
	script.Parent:Destroy()
end)

alot of what he does has to do with other systems, like playing the animation and signaling to another script to store some of his data. really the only thing he does that relates to respawning another version of himself is signaling to the original script that he’s died on line 7.

im sort of at a loss here. everything else I could find on the devforum was about respawn systems in general, not something this bizarrely specific.

any help, or suggestions you could offer would be greatly appreciated. thank you for taking the time to read this :slight_smile:

edit: someone suggested i add the code that actually controls the damage dealing (oops!) so here it is:

local Debounce = {} --begins as nil
--code runs every time a raycast hits the target
caster.HumanoidCollided:Connect(function(RaycastResult, HitHumanoid)
	local damage = (usedStats["baseDamage"] + usedStats["damageBonus"]) * (1 + (usedStats["damagePercent"] / 1000)) --damage calculation
	print(Debounce[HitHumanoid])
	if Debounce[HitHumanoid] == false then 
		return
	end
	Debounce[HitHumanoid] = true
	print(Debounce[HitHumanoid])
	if Debounce[HitHumanoid] == playerHumanoid then return end --returns if it hits the player
	HitHumanoid:TakeDamage(damage)
    --clones and sets location of damage indicator
	local hitPos = RaycastResult.Position
	local activePart = indicatorPart:Clone()
	activePart:SetAttribute("Damage", damage)
	activePart.CFrame = CFrame.new(hitPos.x,hitPos.y,hitPos.z)
	activePart.Parent = workspace	
	Debounce[HitHumanoid] = false
	print(Debounce[HitHumanoid])
end)

the script uses Pyseph’s ClientCast module, which basically raycasts from attachments on the blade and returns data on the position of the hit and the instance that it hit. he made a post here and the documentation is here.

Can we see the sword code? I think this is an issue of that code stacking the Damage value… If not, try other NPC’s/printing how much damage the sword is doing.

1 Like

right, that seems important lel. i added the code to the op and im recording a video right now of the damage output.

alright, here it is. it seems like the debounce works perfectly on the first orc, and then instantly breaks on the second.

id be much obliged for any help :slight_smile:

1 Like