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

How come I keep getting an Instance is locked error?

I've been trying to fix an error for a couple of hours now but it just won't work. It occurs when a player changes a team 3 times. I don't know why it's occurring tho. Can somebody please help me with this?

The Parent property of TanWebbing2 is locked, current parent: NULL, new parent StarterCharacter

Script

EntenteClassEvents.AmericanRifleManEvent.OnServerEvent:Connect(function(Player)
		local AmericanRifleManCharacter = EntentePowers.AmericanRifleMan:Clone()
		if not game.StarterPlayer:FindFirstChild("StarterCharacter") then
			AmericanRifleManCharacter.Name = "StarterCharacter"
			AmericanRifleManCharacter.Parent = game.StarterPlayer
			wait(.1)
			if TanWebbingRandom == 1 then
				Webbings.TanWebbing1.Parent = AmericanRifleManCharacter
				print('Tan 1')
			elseif TanWebbingRandom == 2 then
				Webbings.TanWebbing2.Parent = AmericanRifleManCharacter
				print('Tan 2')
			elseif TanWebbingRandom == 3 then
				Webbings.TanWebbing3.Parent = AmericanRifleManCharacter
				print('Tan 3')
			end
		else
			game.StarterPlayer.StarterCharacter:Destroy()
			AmericanRifleManCharacter.Name = "StarterCharacter"
			AmericanRifleManCharacter.Parent = game.StarterPlayer
			wait(.1)
			if TanWebbingRandom == 1 then
				Webbings.TanWebbing1.Parent = AmericanRifleManCharacter -- occurs here
				print('Tan 1')
			elseif TanWebbingRandom == 2 then
				Webbings.TanWebbing2.Parent = AmericanRifleManCharacter -- occurs here
				print('Tan 2')
			elseif TanWebbingRandom == 3 then
				Webbings.TanWebbing3.Parent = AmericanRifleManCharacter -- occurs here
				print('Tan 3')
			end
		end
	end)

1 comment

0

Is there a line where it destroys TanWebbing? And does the TanWebbing clone when it was parented into AmericanRifleMan?

1 answer

✓ Accepted answer

Took me a while to realize that you destroyed the character.

So what I think is happening is that because you destroyed the StarterCharacter without cloning the TanWebbing, the TanWeb became destroyed as well.

Don't understand it?

Basically, when you destroyed the AmericanRifleManCharacter, you also destroyed the TabWebbing and there's no more TanWebbing to be parented. A way to fix this is to clone the TanWebbing before parenting it.

if not game.StarterPlayer:FindFirstChild("StarterCharacter") then
	AmericanRifleManCharacter.Name = "StarterCharacter"
	AmericanRifleManCharacter.Parent = game.StarterPlayer
	wait(.1)
	if TanWebbingRandom == 1 then
		local ClonedWeb = Webbings.TanWebbing1:Clone() -- Clones the Webbing, so now when it's parented into the StarterCharacter, there will still be a spare.
		ClonedWeb.Parent = AmericanRifleManCharacter
		print('Tan 1')
	elseif TanWebbingRandom == 2 then
		local ClonedWeb = Webbings.TanWebbing2:Clone()
		ClonedWeb.Parent = AmericanRifleManCharacter
		print('Tan 2')
	elseif TanWebbingRandom == 3 then
		local ClonedWeb = Webbings.TanWebbing3:Clone()
		ClonedWeb.Parent = AmericanRifleManCharacter
		print('Tan 3')
	end
else
	game.StarterPlayer.StarterCharacter:Destroy()
	AmericanRifleManCharacter.Name = "StarterCharacter"
	AmericanRifleManCharacter.Parent = game.StarterPlayer
	wait(.1)
	if TanWebbingRandom == 1 then
		local ClonedWeb = Webbings.TanWebbing1:Clone() -- Clones the Webbing, so now when it's parented into the StarterCharacter, there will still be a spare.
		ClonedWeb.Parent = AmericanRifleManCharacter
		print('Tan 1')
	elseif TanWebbingRandom == 2 then
		local ClonedWeb = Webbings.TanWebbing2:Clone()
		ClonedWeb.Parent = AmericanRifleManCharacter
		print('Tan 2')
	elseif TanWebbingRandom == 3 then
		local ClonedWeb = Webbings.TanWebbing3:Clone()
		ClonedWeb.Parent = AmericanRifleManCharacter
		print('Tan 3')
	end
end

The Parent property of TanWebbing2 is locked, current parent: NULL, new parent StarterCharacter basically means TanWebbing2 is nil or destroyed (Or garbage collected) so to prevent that error from happening, we can clone that instance before we use it so that if we need it again, there would be a spare instance for us to use.

2 comments

0

Well explained, upvote

0

I noticed how I forgot a table to put the able that holds all the webbings, but that didn't really make it hard. So I decided to do exactly what you did and did a bit of tweaking to other stuff that kept not working namely the randomizers and it works as intended! Thank you so very much for the help!

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