PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Remove All Objects from Player

Remove All Objects from Player

Jun 23 2018, 2:02 AM PST

Problem

You want to remove multiple limbs without doing a lot of copy and pasting.

Solution

Use a table, and then iterate over that table.

local limbs = {"Right Arm", "Left Arm", "Right Leg", "Left Leg"}
local pl = Workspace:FindFirstChild("Player")
if pl then
	for _, v in ipairs(limbs) do
		local limb = pl:FindFirstChild(v)
		if limb then
			limb:Destroy()
		end
	end
end

Discussion

The first line of code defines a table (a value that holds other values, in this case it holds the strings of types of limbs). The second line of code safely indexes the character and the third line checks if its valid or not. Next is the generic for loop, this is a special type of loop that uses an iterator function to handle the loop. I won’t go into detail about it here, all you need to know is that i is set to the iteration (basically a count of how many times the loop has run), and v is set to the value (it will be the contents of the table, e.g. when i is equal to 1, v will be equal to ‘Right Arm’).

Inside the loop we set the limb variable to the character safely indexing v (the current limb we are at). So the first iteration would safely index Workspace.Player['Right Arm'], the second iteration would do the same for Workspace.Player['Left Arm'], and so forth until the loop ends at the last element in the table. Then the conditional checks if the limb is there, and removes it if so.

The final result is a safe way to remove all the limbs. Even if the player doesn’t exist, the script won’t error, and if a certain limb doesn’t exist, it won’t error.

Tags:
  • humanoid