Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I return a nil if a limb is already blown off?

This script is a landmine script. It requires my r15 ragdoll script. The landmine would launch players' bodies up in the air and blow off limbs by chance. The script would select any random children in the player's character ten times and checks if it's the right limb (e.g. RightUpperArm). The script works fine. However, if it tries to remove a limb's joint that's already been removed, it would error: RagdollRightUpperArm is not a valid member of Model I tried to fix my problem on line 24. But it doesn't work.

touch = true

function onTouch(PART)
	if touch then
		touch = false
		if touch == true then return end
		local humanoid = PART.Parent:FindFirstChild("Humanoid")
		if humanoid ~= nil then
			math.randomseed(tick())
			local sound = script.Parent:WaitForChild("Boom")
			sound:Play()
			humanoid.Health = 0
			PART.Velocity = Vector3.new(math.random(1,10), 500, math.random(1,10))
			PART.RotVelocity = Vector3.new(math.random(10,25), 0, math.random(10,25))
			local function chance(percentage)
    			return math.random() < percentage / 100
			end
			if chance(60) then
				for i = 1, 10 do
					local children = humanoid.Parent:GetChildren()
					wait(0.001)
					local removeJoint = (children[math.random(1,#children)])
					if removeJoint.Name == "RightUpperLeg" or removeJoint.Name == "RagdollRightUpperLeg" then
						if humanoid.Parent.RagdollRightUpperLeg == nil then
							print("Right leg already blown off!")
						end
						humanoid.Parent.RagdollRightUpperLeg:Destroy()
						print("Right leg blown off!")
					end
					if removeJoint.Name == "RightUpperArm" or removeJoint.Name == "RagdollRightUpperArm" then
						if humanoid.Parent.RagdollConstraintRightUpperArm == nil then
							print("Right arm already blown off!")
						end
						humanoid.Parent.RagdollRightUpperArm:Destroy()
						print("Right arm blown off!")
					end
					if removeJoint.Name == "LeftUpperLeg" or removeJoint.Name == "RagdollLeftUpperLeg" then
						if humanoid.Parent.RagdollLeftUpperLeg == nil then
							print("Left leg already blown off!")
						end
						humanoid.Parent.RagdollLeftUpperLeg:Destroy()
						print("Left leg blown off!")
					end
					if removeJoint.Name == "LeftUpperArm" or removeJoint.Name == "RagdollLeftUpperArm" then
						if humanoid.Parent.RagdollLeftUpperArm == nil then
							print("Left arm already blown off!")
						end
						humanoid.Parent.RagdollLeftUpperArm:Destroy()
						print("Left arm blown off!")
					end
				end
			else
  				print("No limbs blown off.")
			end
		end
		wait(1)
		touch = true
	end
end

script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

I'm confused as to how this could error, since it uses the immediate children. Everything should exist. Also there is a lot of redundant syntax and stuff that should be looped. Looks like you can store removeable limbs in a table then look for the limb inside the character to make things simple. However I will write this to keep the system you already have. Let me know if it works lel.

Most importantly, start making use of the function FindFirstChild.

touch = true
canRemove = {"RightUpperLeg","RightUpperArm","LeftUpperLeg","LeftUpperArm"}

function onTouch(PART)
    if touch then
        touch = false
        local char = PART.Parent
        local humanoid = char:FindFirstChild("Humanoid")
        if humanoid then
            math.randomseed(tick())
            local sound = script.Parent:WaitForChild("Boom")
            sound:Play()
            humanoid.Health = 0
            PART.Velocity = Vector3.new(math.random(1,10), 500, math.random(1,10))
            PART.RotVelocity = Vector3.new(math.random(10,25), 0, math.random(10,25))

            if math.random() < 0.60 then --chance
                for i = 1, 10 do
                    wait()
                    local children = char:GetChildren()
                    local removeJoint = children[math.random(#children)]
                    for _,name in pairs(canRemove) do
                        if removeJoint.Name:match(name) then
                            --found limb or ragdoll limb
                            if removeJoint.Name == name then
                                --replace with ragdoll
                                removeJoint = char:FindFirstChild("Ragdoll"..name)
                            end
                            if removeJoint then
                                removeJoint:Destroy()
                            end
                            break
                        end
                    end
                end
            else
                print("No limbs blown off.")
            end
        end
        wait(1)
        touch = true
    end
end

script.Parent.Touched:connect(onTouch)

1 comment

0

Yes, it does work. Thanks! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.