Humanoid TakeDamage Killing Wrong Target

Alright I have a weird bug. Currently I am trying to remove health from a target (in this case a zombie). Yet when I run TakeDamage it kills the player for some reason.
The charT value returns the zombie right before the TakeDamage is run yet the zombie doesn’t take any damage and the player is instantly killed no matter what damage is passed

local charT = script.Target.Value
print(charT) -- Prints Zombie
print(dmg)   -- Prints 85
charT.Humanoid:TakeDamage(dmg) --This kills me (The player (Name Daanemark11))

Anyone know why this might be happening?
I’ve tried the many validations but for some reason the damage is just being ran on the wrong target.

1 Like

Is there anyway you could show us more of the code? It seems like there are some variables outside of this that may need consideration before a conclusion can be made.

I have been working on it and I found this.

The following works if I give the number manually

local dmg = SpellCasting.DecideDamage(damage, DamageType, charT)
charT.Humanoid.Health = charT.Humanoid.Health - tonumber(10)
SpellCasting.DisplayDamage(dmg, charT.Parent.Root, false)

However, if I run it like this

local dmg = SpellCasting.DecideDamage(damage, DamageType, charT)
charT.Humanoid.Health = charT.Humanoid.Health - tonumber(dmg)
SpellCasting.DisplayDamage(dmg, charT.Parent.Root, false)

It just instant kills me instead

This is my DecideDamage function

function module.DecideDamage(damage, damageType, charType)
	--The damage first enetered is 60-100
	if charType:FindFirstChild("Info") then
	if charType:FindFirstChild("Info"):FindFirstChild("Type").Value == damageType then
		local reduced = math.ceil(0.8 * damage)
		return reduced
		return damage
		return damage

Where if the target has a value info attached to it with the same type as the given type they take reduced damage

I see. What is the initial value for the variable “damage?”

I get the value damage like this

local damage = CardTable.GetDamageBasedOnName(script.Parent.Name)
function module.GetDamageBasedOnName(index)
	local card = module.ReturnCardByName(index)
	local list = card["Amount"]
	return math.random(list[1], list[2])

Where it gets the values from this table

Grave = {
		Name = "Grave",
		LongName = "Grave",
		Image = "rbxassetid://4528807667",
		CastCost = 1,
		Rarity = 0,
		Accuracy = 90,
		Type = "Damage",
		TypeImage = "rbxassetid://4525874206",
		Amount = {65, 105},
		--CardType = "Undead",
		CastType = "Undead",
		CastTypeImage = "rbxassetid://4525871560",
		Target = "Single",
		Desc = "65 - 105 Undead damage.",
		Member = false,

So it has something to do with dealing more damage than the target has hp for. If I manually enter (24) in the damage dealt. It will run perfectly fine. However, if I set it to 25 (the zombies health). The player is instantly killed.

I can’t seem to reproduce your issue.

You might want to show more code so we can understand what is happening.

Thanks for the help!

Though, for some reason dealing damage that brings the target below the hp thresh holds kills the player so instead. If the damage is more than their health it now just removes the target from play and that seems to work.

Is there any script or code altering the player health in your scripts?

The issue might be there, you should search there. Preferably post the snippets that affect player health here.

Though, for now, as band aid fix, you may want to limit the damage dealt to a value between 0 and the zombie’s health - 1.

You can do this by clamping:

math.clamp(damage, 0, charT.Humanoid.Health - 1)

damage is assigned from the return from GetDameBasedOnName which is 2 items list[1] and list[2]
To me that implies damage is a table containing two items lowest and highest damage so anywhere you use damage you perhaps should refer to damage[1].

That is a great point I will look into that!

This indeed works! I will use it for now but if I find a way to do it differently aftwards I will