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

It's supposed to clone a tool and put it in the player's backpack, but it doesn't. Help please?

Asked by 4 years ago

Can anyone help me with this code? It only clones the tool and not the scripts and stuff inside the tool.

local button = script.Parent
local tool = script.Parent:FindFirstChild("Rifle")
local tool2 = script.Parent:FindFirstChild("Shovel")
local plr = game.Players.LocalPlayer

button.MouseButton1Click:Connect(function()
    tool:Clone().Parent = plr.Backpack
    tool2:Clone().Parent = plr.Backpack
    script.Parent.Parent.Visible = false
    script.Parent.Parent.Parent.Choose.Visible = false
end)

Any help would be appreciated!

0
I added a little bit more to the bottom of the answer, so please take a look at it if you haven't already. Brandon1881 721 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

From what I can see, it's most likely that the tools aren't there when the script loads in, so the script doesn't know what tool or tool2 is.

To fix this, simply use WaitForChild. This will wait for the tools to load in before continuing the code.

Example:

local tool = script.Parent:WaitForChild("Rifle")
local tool2 = script.Parent:WaitForChild("Shovel")

However, if the tools are not in the correct location where you are referencing it from, there will be an infinite yield. So make sure that the tools have the same parent as the script.

Also, I would not use a local script for this because if the game has FE turned on (which it should), then the tools will not be seen by other players, so I would recommend to use a script for this. You can define the player by using something like local plr = script.Parent.Parent.Parent, but you'd have to figure out how many .Parents you need.

Ad

Answer this question