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

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

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

3 comments

0

Can you be more specific, such as giving us the actual error?

0

that is the actual error

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

3 answers

✓ Accepted answer

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

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.

2 comments

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.

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.)

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()

2 comments

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

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.

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