Script only affect 1 part and that's all

Hello forums, i am experiencing this issue lately and still didn’t know how to solve it yet. Basically, i want to make a limbs health script that add attribute to player’s part, the attribute value will be lowered when player take damage and eventually reach 0. I thought the script went well because it affect all parts which is what i wanted .However, the further i went, i start to notice the only part that was affected was the Left Arm part, it start from most of the time to all the time. I tried to fix it but i couldn’t. Any help to fix the script or optimizing it is appreciated! (There’s no error, i’ve fixed every error i could, Local script).

Here’s the script:

--- Prefering and setting:
local player = game:GetService("Players")
local char = player.LocalPlayer.Character
local humanoid = char:WaitForChild("Humanoid")
local OldHealth = humanoid.Health
local LArm = char:WaitForChild("Left Arm")
local originalLarmhp = 100
LArm:SetAttribute("Health", 100)
local RArm = char:WaitForChild("Right Arm")
local originalRarmhp = 100
RArm:SetAttribute("Health", 100)
local LLeg = char:WaitForChild("Left Leg")
local originallleghp = 100
LLeg:SetAttribute("Health", 100)
local RLeg = char:WaitForChild("Right Leg")
local originalRleghp = 100
RLeg:SetAttribute("Health", 100)
local Head = char:WaitForChild("Head")
local originalheadhp = 100
Head:SetAttribute("Health", 100)
local Torso = char:WaitForChild("Torso")
local originaltorsohp = 100
Torso:SetAttribute("Health", 100)
local chance = script.Chance
local dmg = script.Damage

--- Function
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if humanoid.Health < OldHealth then
		if chance.Value == 1 then
			LArm:SetAttribute("Health", originalLarmhp - dmg.Value)
		if chance.Value == 2 then
			RArm:SetAttribute("Health", originalRarmhp - dmg.Value)
		if chance.Value == 3 then
			LLeg:SetAttribute("Health", originallleghp - dmg.Value)
		if chance.Value == 4 then
			RLeg:SetAttribute("Health", originalRleghp - dmg.Value)
		if chance.Value == 5 then
			Head:SetAttribute("Health", originalheadhp - dmg.Value)
		if chance.Value == 6 then
			Torso:SetAttribute("Health", originaltorsohp - dmg.Value)
		end
		end
		end
		end
		   end
		end
	end
end)

--- randomizing the value.
while true do 
	wait(0.01)
	chance.Value = math.random(0, 6)
	dmg.Value = math.random(5, 25)
	originalLarmhp = LArm:GetAttribute("Health")
	originalRarmhp = RArm:GetAttribute("Health")
	originallleghp = LLeg:GetAttribute("Health")
	originalRleghp = RLeg:GetAttribute("Health")
	originaltorsohp = Torso:GetAttribute("Health")
	originalheadhp = Head:GetAttribute("Health")
end
1 Like

The if statements are nested, all of the body parts except for the left arm can only be checked if the left arm is chosen (which obviously won’t succeed). You have to change the if statements to elseif statements:

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if humanoid.Health < OldHealth then
		if chance.Value == 1 then
			LArm:SetAttribute("Health", originalLarmhp - dmg.Value)
		elseif chance.Value == 2 then
			RArm:SetAttribute("Health", originalRarmhp - dmg.Value)
		elseif chance.Value == 3 then
			LLeg:SetAttribute("Health", originallleghp - dmg.Value)
		elseif chance.Value == 4 then
			RLeg:SetAttribute("Health", originalRleghp - dmg.Value)
		elseif chance.Value == 5 then
			Head:SetAttribute("Health", originalheadhp - dmg.Value)
		elseif chance.Value == 6 then
			Torso:SetAttribute("Health", originaltorsohp - dmg.Value)
		end
	end
end)

If you use the original code and go to the top bar, click on “Format Selection” and then “Format Document”, you would see how the if statements are actually nested:

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if humanoid.Health < OldHealth then
		if chance.Value == 1 then
			LArm:SetAttribute("Health", originalLarmhp - dmg.Value)
			if chance.Value == 2 then
				RArm:SetAttribute("Health", originalRarmhp - dmg.Value)
				if chance.Value == 3 then
					LLeg:SetAttribute("Health", originallleghp - dmg.Value)
					if chance.Value == 4 then
						RLeg:SetAttribute("Health", originalRleghp - dmg.Value)
						if chance.Value == 5 then
							Head:SetAttribute("Health", originalheadhp - dmg.Value)
							if chance.Value == 6 then
								Torso:SetAttribute("Health", originaltorsohp - dmg.Value)
							end
						end
					end
				end
			end
		end
	end
end)
3 Likes

Very useful information, thank you man.

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