Problems with script logic with tools

Hi, I’m trying to make a bodypart health system where if I were to strike my sword (tool) onto a person, the part where it hits will get damage. (R15 character)
I’m quite new to scripting so bear with me.

if TOUCHED.Name == "RightHand" or "RightUpperArm" or "RightLowerArm" then
								local limbhealth = TOUCHED.Parent:WaitForChild("RightArm")
								limbhealth.Value = limbhealth.Value - 1
								print("rightlimb")
								print(TOUCHED.Name.. "right")
								
							else
								
								if TOUCHED.Name == "LeftHand" or "LeftUpperArm" or "LeftLowerArm" then
									local limbhealth = TOUCHED.Parent:WaitForChild("LeftArm")
									limbhealth.Value = limbhealth.Value - 1
									print("leftlimb")
									print(TOUCHED.Name.. "left")
									
								else
									
									HUM:TakeDamage(1)
									print("dmgtaken")
									print(TOUCHED.Name)
								end
				
							end

The issue is that if the hit is another bodypart, it would still register as the first “if” line. So if I were to hit the left limb, it’d still take damage on the right limb’s IntValue.

u gotta specify the others, do the same with the right hand

if TOUCHED.Name == "LeftHand" or TOUCHED.Name == "LeftUpperArm" or TOUCHED.Name == "LeftLowerArm" then

-- script here 

end
1 Like

Ooh, while I definitely missed this one, now this ‘if’ sentence doesn’t register at all except for the take humanoid damage at the very last

The problem with this script is in the first line, the if conditional. Basically, you’re saying that if the touched part is Right hand OR the string RightUpperArm etc. exists, which will always be true. If you want an easy solution, here:

local Parts = {
"RightHand",
"RightUpperArm"
}

if table.find(Parts, TOUCHED.Name) then
   -- Code
end
1 Like

OK, I’ve just changed my script into using Raycasting for my script but it works similar to Touched because why not but the process is similar. Is this what you’re trying to get me to do? Here’s the script but it doesn’t register the ‘if’ sentence and goes straight to the else line. (sorry table isn’t a very strong topic for me)

newHitbox.OnHit:Connect(function(hit, humanoid)
				print(hit)
				local Parts = {
					"LeftHand",
					"LeftUpperArm",
					"LeftLowerArm"
				}

				if table.find(Parts, hit) then
					humanoid.Parent.LeftArm.Value = humanoid.Parent.LeftArm.Value - 10
					print("left")
					
				else
					humanoid:TakeDamage(5)
				end
				
			end)

Yes, it’ll compare the name with names in a table. Also, what is OnHit?

It’s similar to Touched event but for a raycasting module I used. Raycast Hitbox 4.01: For all your melee needs!

However, it still doesn’t work and skips to the humanoid:TakeDamage(5)

What does it print in the print(hit)?

image

Bodyparts.However, even if I hit LeftUpperArm, it doesn’t go for the LeftArm line

Ah, found the mistake.

Change

if table.find(Parts, hit) then

to

if table.find(Parts, hit.Name) then

You’re looking for the actual Instance in the table, not the name.

1 Like

It works now! Thank you! (30char)

1 Like