Table stops working after an object it references gets destroyed

Hi, my script will stop functioning and error “Example is not a valid member of “Example””. Here is my script:


I know what the problem is, but am not sure how to fix it.
Ive tried adding this code to it, but it errors the same thing:

for i,v in pairs(PlayerLimbs) do
			if v == nil then
				table.remove(PlayerLimbs, v)
			end
		end

All help is appreciated!

I am still reading your code, but instead of using workspace[player.Name] you can just replace that with player.Character.

It might be easier if you could tell me what you replaced with ‘example’.

"Left arm is not a member of "

“Left arm is not a member of (player name)”

Screen Shot 2021-10-15 at 12.25.04 PM
No spaces in the name! You gotta choose from one of these. I believe that if you destroy some specific items, the humanoid instantly dies. (Probably head, torso, humanoidrootpart)

I put forced the game to make players in r6 so there would be less parts to destroy, in r6 I believe that there are spaces in the names.

Case-sensitive. Make sure to capitalize your words! Screen Shot 2021-10-15 at 12.28.35 PM

Edit: I saw in your code you did capitalize, I’ll try and find another error.

Yeah, you are using the correct names (oops forgot to check if there are any limbs at all, fixed)

Possibly fixed version:

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if not player.Character then return end

	local character = player.Character

	local limbs = {
		character:FindFirstChild("Left Arm");
		character:FindFirstChild("Right Arm");
		character:FindFirstChild("Left Leg");
		character:FindFirstChild("Right Leg");
	}
	
	if #limbs < 1 then return end

	local limb = limbs[math.random(1, #limbs)]

	if limb then
		limb:Destroy()
		table.remove(limbs, table.find(limbs, limb))
	end
end)
2 Likes

I believe that is correct. Once the limb has been removed, there is still a chance that the math.random() function may still choose the destroyed limb. You have to remove it from the table.

FindFirstChild will return nil if it found nothing, therefore it won’t break since Destroy has already got rid of it (setting a value to nil removes the value from the table as far as i know)