Warn keeps printing

I am working on overhauling my games combat and with that comes a limb management system (essentially a module script that controls limb loss, limb health values, and limb damage through functions which are called in a server script in the startercharacter). I have limb damage setup using string values to determine the health of a limb (located inside the respective limb) and I want to have a warn appear when you fire the limb damage function with an invalid limb. Only problem is, it warns every time the function is fired even if its valid.

I tried checking to see if the limb you reference is not named a valid limb but it still prints every time, any solutions? Here is the function:

function LimbHandler.DamageLimb(char, limb, amount)
	local Descendants = char:GetDescendants()
	for _, v in pairs(Descendants) do
		if v:IsA("StringValue") then
			if v.Name == limb then
				v.Value = v.Value - amount
				print("Damaged " .. v.Name .. "by " .. amount)
			elseif limb ~= "Left Arm" or "Right Arm" or "Left Leg" or "Right Leg" or "Head" then
				warn(limb .. " Is not a valid limb!")
			end
		end
	end
end

Try putting a single if statement at the top of the function (still inside the function) that contains if limb ~= "Left Arm" or "Right Arm" or "Left Leg" or "Right Leg" or "Head" then warn(limb .. " Is not a valid limb!") end and removing the elseif at the bottom.

1 Like

This will always return true, since it’s essentially asking “Does this string that I just created exist?” You need to compare limb to each string individually.

1 Like

How would I achieve this? (character limit)

elseif limb ~= "Left Arm" or limb ~= "Right Arm" -- blah blah blah
2 Likes

Still does not work

function LimbHandler.DamageLimb(char, limb, amount)
	if limb ~= "Left Arm" or "Right Arm" or "Left Leg" or "Right Leg" or "Head" then 
		warn(limb .. " Is not a valid limb!")
		local Descendants = char:GetDescendants()
		for _, v in pairs(Descendants) do
			if v:IsA("StringValue") then
				if v.Name == limb then
					v.Value = v.Value - amount
					print("Damaged " .. v.Name  .. "by " .. amount)
				end
			end
		end
	end
end
1 Like

This worked perfectly, thank you.

Sorry, but I made an accidental typo with the or, please fix it.

You should use a function for the sake of repetition. If you are comparing one string to another you can use string.match() or make a special limb only comparator function so you only need to do

if compare("Left Arm") and compare("Right Arm") and...

And that way if you need to compare another string you have a true/false return value and you do not need to keep typing out limb == “stringHere” and worry that it might not be comparing a string.

1 Like