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

How do i fix "attempt to index nil with parent"?

Asked by 3 years ago
script.Parent.OnServerEvent:Connect(function(plr)

    local clone1 = plr.Character:Clone()
    clone1.Parent = workspace
    clone1.HumanoidRootPart.Anchored = false
    clone1.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame
    clone1.Name = plr.Name.."'s Clone"
    clone1.HumanoidRootPart.CFrame = clone1.HumanoidRootPart.CFrame
    game.Debris:AddItem(clone1,10)
end) 

this is my script and everytime i run it the error "attempt to index nil with parent" shows up and i dont know how to fix it

0
Can you be more specific, such as giving us the actual error? DeceptiveCaster 3761 — 3y
0
that is the actual error soreno2468 31 — 3y
0
This is because plr.Character cannot be cloned because it is not archivable, you have to turn archivable off every time the character loads AnasBahauddin1978 715 — 3y

3 answers

Log in to vote
0
Answered by
Optikk 499 Donator Moderation Voter
3 years ago
Edited 3 years ago

This is due to the fact that player characters are not Archivable by default. If an Instance is not archivable, something:Clone() returns nil.

The solution would be to make the character archivable.

OnServerEvent:Connect(function(player)
    local character = player.Character
    character.Archivable = true
    local clone = character:Clone()
    -- etc
end
Ad
Log in to vote
0
Answered by 3 years ago

Unfortunately, the only way to clone a character is to use Players:GetCharacterAppearanceAsync or Players:GetCharacterAppearanceInfoAsync. I'll shorten these to GetCharacter and GetCharacterInfo because they're hard to type.

GetCharacter returns a model full of these assets. Keep in mind that it does not return an NPC model. It returns the assets. So if you wanna get the Appearance, do this:

script.Parent.OnServerEvent:Connect(function(plr)
    local model = game.Players:GetCharacterAppearanceAsync(plr.UserId) --You need a UserId.
    model.Parent = workspace --Recommend you parent it to workspace to see the properties. This is only an example.
end)

However, if you prefer a table, use GetCharacterInfo. However, it uses Dictionaries and sub-assets, which makes it harder to use. Don't recommend this one unless you don't wanna spam assets.

script.Parent.OnServerEvent:Connect(function(plr)
    local table = game.Players:GetCharacterAppearanceInfoAsync(plr.UserId)
    for i, v in pairs(table) do
        print(i , v) --Recommend you do to get the table of contents.
    end
end)

Also, turning off Archivable may result in exploits on the client. So I don't recommend it at ALL.

0
Do you mind elaborating on that conjecture of yours, that turning Archivable on leads to exploits? I don’t think this is true and I’d like to know your source. Optikk 499 — 3y
0
I'm pretty sure if roblox want to enable Archivable, they don't want anyone to mess with the character. I am not exactly sure, but, it may lead to exploits, considering non-archivable resources can be exploited (on the client especially.) Dovydas1118 1495 — 3y
Log in to vote
-2
Answered by 3 years ago

Grab the player from the actual location.

To grab a certain user to clone:

local plrModel = game.Workspace:WaitForChild("--input here")

To grab the player model:

local plrModel2 = game.Players.LocalPlayer:GetChildren()
0
This isn't even the reason why it's not working on top of that, terrible methods what if plrModel was a part in workspace and game.Players.LocalPlayer:GetChildren() doesn't return the character greatneil80 2647 — 3y
0
It's nice of you to attempt to help someone, but please don't post an answer if you have no idea what you're talking about. Optikk 499 — 3y

Answer this question