Script not stopping code even after value is false

I have a physics damage script that activates when the value is set to true, aka ragdolled. Before you even ragdoll, the script works fine, not damaging the limb health regardless of falling from a height. When you ragdoll, it works as intended however if you unragdoll after you ragdolled and just fall from a high place, you will still take limb damage. Now, in the script, it’s set to print out a debug message when Ragdolled.Value == false and it does print, but the limb damage still happens eitherway.

Script:

wait(0.1)
local RagdolledBool = script.Parent.Ragdolled

RagdolledBool:GetPropertyChangedSignal("Value"):Connect(function()
	if RagdolledBool.Value == true then
		for i, child in ipairs(script.Parent:GetDescendants()) do
			if child:IsA("Part") then
				child.Touched:connect(function(hit)
					if hit.Name ~= "Handle" and child.Name ~= "Handle" and child.Name ~= "Torso" and child.Name ~= "HumanoidRootPart" and hit.Parent ~= script.Parent and child.Velocity.Magnitude >= 50 then
						child.BoneHP.Value = math.round(child.BoneHP.Value - child.Velocity.Magnitude)
						child.LimbJointHP.Value = math.round(child.LimbJointHP.Value - child.Velocity.Magnitude/4)
						child.LimbPhysicalHP.Value = math.round(child.LimbPhysicalHP.Value - child.Velocity.Magnitude/6)
					end
				end)
			end
		end
	else
		print("no more")
	end
end)

Thanks to anyone who helped me.

This event is never disconnected, so each time the Ragdollbool value is changed, it runs this thread

How would I be able to disconnect it?

wait(0.1)
local Connections = {} -- touched connections to keep track of
local RagdolledBool = script.Parent.Ragdolled

RagdolledBool:GetPropertyChangedSignal("Value"):Connect(function()
    for _, connection in Connections do
        connection:Disconnect() -- Disconnect old connections
    end
    table.clear(Connections) -- Remove all the old connections from the table to free up memory

	if RagdolledBool.Value == true then
		for i, child in ipairs(script.Parent:GetDescendants()) do
			if child:IsA("Part") then
				table.insert(Connections, child.Touched:Connect(function(hit) -- This adds the RBXScriptConnection to the table
					if hit.Name ~= "Handle" and child.Name ~= "Handle" and child.Name ~= "Torso" and child.Name ~= "HumanoidRootPart" and hit.Parent ~= script.Parent and child.Velocity.Magnitude >= 50 then
						child.BoneHP.Value = math.round(child.BoneHP.Value - child.Velocity.Magnitude)
						child.LimbJointHP.Value = math.round(child.LimbJointHP.Value - child.Velocity.Magnitude/4)
						child.LimbPhysicalHP.Value = math.round(child.LimbPhysicalHP.Value - child.Velocity.Magnitude/6)
					end
				end))
			end
		end
	else
		print("no more")
	end
end)
1 Like

Thank you so much, and thank you for adding little comments and explaining.