Sword instakills but is only supposed to do 40.4 damage?

I’m trying to make a sword for my game but it ends up instakilling anything to touches for some reason.

This is my script.

--variables--
box = script.Parent
tool = box.Parent
cooldown = tool.Cooldown.Value
damage = tool.Damage.Value
debounce = false
yourchar = tool.Parent
yourplr = game:GetService("Players"):GetPlayerFromCharacter(yourchar)
dmgeffect = game.ReplicatedStorage.DamageHighlight


--script--

box.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		--find the character box touched
		local char = hit.Parent
		--find the player of the character
		local player = game.Players:GetPlayerFromCharacter(char)
		local def = char.Humanoid.DefenseValue.Value/100
		local totaldmg = damage * tool.Parent.Humanoid.AttackValue.Value / def
		--damage the player
		if player then

			if player.TeamColor == yourplr.TeamColor then
			else
			player.Character.Humanoid.Health = player.Character.Humanoid.Health - totaldmg	
				dmgclone = dmgeffect:Clone()
				dmgclone.Parent = char
				dmgclone.Script.Enabled = true
			
--if the player they hit died, increase yourplr.leaderstats.Kills by 1
			if player.Character.Humanoid.Health <= 0 then
				yourplr.leaderstats.Kills.Value = yourplr.leaderstats.Kills.Value + 1
					yourplr.leaderstats.Streak.Value = yourplr.leaderstats.Streak.Value + 1

			end	
			end
		
		elseif not player then
			char.Humanoid.Health = char.Humanoid.Health - totaldmg
			
			dmgclone = dmgeffect:Clone()
			dmgclone.Parent = char
			dmgclone.Script.Enabled = true		
			if char.Humanoid.Health <= 0 then
				yourplr.leaderstats.Kills.Value = yourplr.leaderstats.Kills.Value + 1
				yourplr.leaderstats.Streak.Value = yourplr.leaderstats.Streak.Value + 1
				
			end	

		end
		
		wait(cooldown)
		debounce = false
	end
end)

I had the video’s damage as 1 because i was testing to see if there was some multiplier that i added by accident.

use :takedamage() instead of player.Character.Humanoid.Health = player.Character.Humanoid.Health - totaldmg

What does totaldmg come out to?

The base damage of the tool, multiplied by the player’s AttackMultiplier and then divided by the target’s DefenseValue, divided by 100 (for percentage)

Dividing the final damage value by a defence percentage would does not dampen the damage at maximum defence. You would simply divide the final damage value by 1, which equates to the final damage value. On the other hand, having no defence would result in a zero-division, in which Luau outputs inf. It is likely that the NPC is receiving this infinite damage.

Ah, i never checked it. I used print(totaldmg) and it came out as inf. Have any idea how i should change the math? I’m trying to make it kind of like Item Asylum’s damage concept. Item has a base damage, then it gets multiplied by a value that you have, then some damage is taken away based on the percent of the target’s defense number.

You mean to factor in defence like so:

baseDamage * damageMultiplier * (1 - defence / maxDefence)

It worked!! Thanks a lot, I had been stumped on this for a while.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.