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

I need help on fixing 'Workspace.Part.Script:8: attempt to index nil with 'Backpack'?

Asked by 3 years ago

Ok, so basically I'm scripting a thing that doesnt clone the tool, it sets a new parent.

The script:

local robot = script.Parent.Robot
local starterpack = game.StarterPack
local Players = game:GetService("Players")



script.Parent.ClickDetector.MouseClick:connect (function(plr)
        robot.Parent = game.Players.LocalPlayer.Backpack

end)

As in output, when i click. It says: Workspace.Part.Script:8: attempt to index nil with 'Backpack

Can somebody help?

3 answers

Log in to vote
0
Answered by
ghxstlvty 133
3 years ago

I can't help you with your real question but consider using the advice below.

connect is deprecated use "Connect", remove the space in between connect and the function.

local robot = script.Parent.Robot
local starterpack = game.StarterPack
local Players = game:GetService("Players")



script.Parent.ClickDetector.MouseClick:Connect(function(plr)
        robot.Parent = game.Players.LocalPlayer.Backpack

end)

Also, why did you make a variable for starterpack but didn't use it?

And the final question, is "robot" a tool?

0
Yes, robot is a tool, I forgot I addded a starterpack variable but will remove it. Just noting about the starterpack, the player would have to reset to have it in the backpack. usmaniusmangamer15 9 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

LocalPlayer is not a valid child in any regular server script. This might help a bit:

local robot = script.Parent.Robot

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    local clone = robot:Clone()
    clone.Parent = plr.Backpack
end)
Log in to vote
0
Answered by
Pupppy44 671 Moderation Voter
3 years ago

You can't get the LocalPlayer in a normal script. MouseClick's first variable is the player who clicked, so you can clone the tool to the player's backpack.

local robot = script.Parent.Robot
local starterpack = game.StarterPack
local Players = game:GetService("Players")



script.Parent.ClickDetector.MouseClick:Connect(function(plr)
        robot.Parent = plr.Backpack -- Here's the change

end)

Hope this helps!

Answer this question