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

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

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

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?

1 comment

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.

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)

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!

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