Limb check not working

I have 2 problems, I’m working on a limb dismemberment system and I am working on a function that checks if a stringvalue inside the players limbs is equal to 0, then if it is it removes the limb. Except it doesn’t work at all, and when you lose the limb through other means it constantly displays an error that the limb is not a member of the character.
The strings names are the same as the limbs name.

I tried checking if the limb in question is not nil but that didn’t seem to work either, I honestly have no idea why this isn’t working and how to check if a limb is not nil in this situation.
Here is the code:

local function checkLimbs()
	if char["Left Arm"]["Left Arm"].Value == 0 and char["Left Arm"] ~= nil then
		print("Removing left arm")
		LimbHandler.LoseLimb(char, "Left Arm")
	elseif char["Right Arm"]["Right Arm"].Value == 0 then
		print("Removing Right arm")
		LimbHandler.LoseLimb(char, "Right Arm")
	elseif char["Head"]["Head"].Value == 0 then
		print("Removing Head")
		LimbHandler.LoseLimb(char, "Head")
	elseif char["Left Leg"]["Left Leg"].Value == 0 then
		print("Removing Left leg")
		LimbHandler.LoseLimb(char, "Left Leg")
	elseif char["Right Leg"]["Right Leg"].Value == 0 then
		print("Removing Right leg")
		LimbHandler.LoseLimb(char, "Right Leg")
	end
end

runservice.Stepped:Connect(checkLimbs)

From this I can tell you that you need to be comparing these values to a string, not a number.

if char["Left Arm"]["Left Arm"].Value == 0 and char["Left Arm"] ~= nil then

should be:

if char["Left Arm"]["Left Arm"].Value == "0" and char["Left Arm"] ~= nil then

As well as in all other areas where you’re comparing your string value to 0.

This seems to have fixed it not working at all, but how could I make this happen just once instead of every frame? Could I use something besides runservice? Because it still spams an error in output about the limb not existing after its removed and it applies the limb loss effects multiple times.

You could instead listen for changes to your StringValue with the StringValue | Roblox Creator Documentation signal.

Could I filter through the players descendants like this?

local Descendants = char:GetDescendants()
for _,v in pairs(Descendants) do
	if v:IsA("StringValue") then
		if v.Name == "Left Arm" or v.Name == "Right Arm" or v.Name == "Head" or v.Name == "Left Leg" or v.Name == "Right Leg" then
			v.Changed:Connect(checkLimbs)
		end
	end
end

Or should I just rewrite the function to work like that?

That could work. You don’t want to check all limbs for each limb change though, you’d only want to check that specific limb, so you would want to modify your function to work for that.

1 Like

Yeah I noticed, everything works until another limbs health goes to 0 then it shows an error. Thanks for the help.

local Descendants = char:GetDescendants()
for _, v in pairs(Descendants) do
	if v:IsA("StringValue") then
		v.Changed:Connect(function()
			if char:FindFirstChild(v.Value) then
				LimbHandler.LoseLimb(char, v.Value)
			end
		end)
	end
end

Or you could just cut it to down to this.

Yeah I actually changed it to work like this already which fixed all the issues but thanks for the help!