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

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

Asked by 5 years ago
Edited 5 years ago

This question has been solved by the original poster.

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)

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). gullet 471 — 5y
0
It did find a child, it printed x.Name (which is the player's name) holamii2 45 — 5y
0
Why do you have two random links that lead to nowhere? DeceptiveCaster 3761 — 5y
0
Figured out that characters aren't archivable, so I made the character archivable before I cloned it. holamii2 45 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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

Answer this question