Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 2 months ago
Edited 2 months 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 403 — 2mo
0
It did find a child, it printed x.Name (which is the player's name) holamii2 -7 — 2mo
0
Why do you have two random links that lead to nowhere? RobloxWhizYT 2026 — 2mo
0
Figured out that characters aren't archivable, so I made the character archivable before I cloned it. holamii2 -7 — 2mo

1 answer

Log in to vote
1
Answered by 2 months 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