Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How come I keep getting an Instance is locked error?

Asked by 2 years ago

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)
0
Is there a line where it destroys TanWebbing? And does the TanWebbing clone when it was parented into AmericanRifleMan? NotThatFamouss 605 — 2y

1 answer

Log in to vote
3
Answered by 2 years ago

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.

0
Well explained, upvote Antelear 185 — 2y
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! vincentthecat1 199 — 2y
Ad

Answer this question