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

a fix to attempt to index local 'clone' (a nil value)?

So I was trying to make a clone of yourself if you click a button, but I get the error "Workspace.Part.ClickDetector.Script:8: attempt to index local 'clone' (a nil value)"

game.Players.PlayerAdded:Connect(function(player) local name = player.Name

script.Parent.MouseClick:connect(function() local x = workspace:FindFirstChild(name) print (x.Name) local clone = x:Clone() clone.Parent = game.Workspace end) end)

4 comments

0

That means the value of clone is nil, and trying to do nil.Parent will error. FindFirstChild probably returned nil (if no child of that name can be found).

0

It did find a child, it printed x.Name (which is the player's name)

0

Why do you have two random links that lead to nowhere?

0

Figured out that characters aren't archivable, so I made the character archivable before I cloned it.

1 answer

✓ Accepted answer

General Practice

Use :GetService() to retrieve the Players Service

Use the CharacterAdded Event of the Player Instance to get the model, rather than FindFirstChild()

game.Workspace can be shortened to workspace

connect is deprecated in favor of Connect

Issues

When the player's Character is created, you can only :Clone() it if its Archivable property is manually set to true, since otherwise, as you've seen, the Instance's Clone function will return nil

Revised Server Script

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(x)
	    x.Archivable = true
	    script.Parent.MouseClick:Connect(function()
		    local clone = x:Clone()
		    clone.Parent = workspace
	    end)
    end)
end)

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