Sword only damages one NPC and not other NPC's

Hello there!

I’m working on a Terraria clone game and there is an issue with a sword I’m currently working on. For some reason, it only damages one NPC only. It also seems to damage me if I change the code a bit as seen here:


--//Tool\\--
local Tool = script.Parent
local RemoteEvent = Tool:WaitForChild("RemoteEvent")
--//Func\\--
RemoteEvent.OnServerEvent:Connect(function(plr,Type)
	if Type == "SwordHitbox1" then
		local Hitbox = Instance.new("Part",workspace)
		game:GetService("Debris",0.01)
		Hitbox.Size = Vector3.new(4,5,6)
		Hitbox.Massless = true
		Hitbox.Transparency = 0.5
		Hitbox.Anchored = false
		Hitbox.CanCollide = false
		Hitbox.CFrame = plr.Character.PrimaryPart.CFrame * CFrame.new(0,0, ((Hitbox.Size.Z)/2 + 1) * -1)
		
		local HitboxWeld = Instance.new("WeldConstraint",Hitbox)
		HitboxWeld.Part0 = plr.Character.PrimaryPart
		HitboxWeld.Part1 = Hitbox
		
		local PartsInsideHitbox = workspace:GetPartsInPart(Hitbox)
		for i,v in pairs(PartsInsideHitbox) do
			if v.Parent:IsA("Model") and (v.Parent ~= plr) then
				
				local Hits = {}
				local Humanoid = v.Parent:FindFirstChildWhichIsA("Humanoid")
				if Humanoid and not Hits[v.Parent] then
					Hits[v.Parent] = true
					Humanoid:TakeDamage(80)
					task.wait(50)
				end
				
			end
		end
		Hitbox:Destroy()
	end
end)
1 Like

I recommend using a module for actual combat hitboxes, which is going to be better for this type of things, but going back to your issue you have to check if the parts that are being hitted arent from your character

1 Like

You should feel proud of the code you have made since it really is good code and it shows that you have been programming for a while.
First of all in the part where you compare
V.Parent~= plr change it to V.Parent ~= plr.Character, and for the other thing remember that the loop goes through all the parts that the hitbox touches, it probably goes through those of a character first and adds them to the table which is a problem since when he goes to go through the other character’s other table, those parts are already found on the table so it will not take his life

1 Like

Task.wait(50) why? This is the razón

1 Like

Idk I had to add since it ruins the code for some reason. Still, I’ve decided to go with @i_iiAngel’s solution since I’m now using a Raycast Hitbox module (Raycast Hitbox 4.01: For all your melee needs!)

1 Like

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