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

How would one insert a tool in a LocalPlayer's backpack via script?

Asked by 4 years ago

I've been having this issue in my tree-cutting game where I'm trying to give the player wood that he/she can carry around. (Don't worry, there's a lot more to the game than that) so, I tried many different techniques to do this. Unfortunately, like all of my scripts, they didn't work.

So what happens is: The player clicks on the tree and the tree disappears until it regnerates. I want to make it so that the player recieves a block of wood for every tree they cut down.

I also used Instance.new to model a tool handle because I figured it wouldn't be that difficult because it's literally just a block of wood.

Here's my code edit it all you want.

01local pines = game.Workspace.Game.Forests.PinePoint --Pine Point is the name of the forest and Forests is the folder.
02 
03local pine = pines:FindFirstChild(pines.PineTree)
04local tool = Instance.new("Tool")
05    tool.Name = "Pine"
06local handle = Instance.new("Part")
07    handle.Parent = tool
08    handle.Material = Enum.Material.Wood
09    handle.BrickColor = BrickColor.new("Pine Cone")
10 
11 
12pines.ChildRemoved:Connect(function()
13    local function giveTool(player, tool)
14        local backpack = player:FindFirstChildOfClass("Backpack")
15        if backpack then
16            tool.Parent = backpack
17        end
18    end
19end)

(go easy on me with the obvious answers)

0
I don't understand why you'd be defining a function inside of an anonymous function, but it's your script. ParticularlyPenguin 71 — 4y

1 answer

Log in to vote
0
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago
Edited 4 years ago

There's a few issues that I can see. First off, it seems that you haven't defined the "player" variable in the local function. From what I assume you are trying to do, if you wanted to fix this you'd have to change basically everything in your script. You'd have to put the script in the ClickDetector script.

Here's an example of what you could do.

01local clickDetector = script.Parent.ClickDetector -- Assumes that the script is in a model which has the ClickDetector
02local clickLimit = 5
03 
04clickDetector.MouseClick:Connect(function(player)
05    clickLimit = clickLimit - 1
06    if clickLimit <= 0 then
07        local tool = Instance.new("Tool") -- Create a new tool
08        tool.Name = "Pine"
09        local wood = Instance.new("Part") -- Create the wood / handle for the tool
10        wood.Parent = tool
11        wood.Name = "Handle" -- Name the wood "Handle" to make sure it is the handle of the tool
12        wood.Material = Enum.Material.Wood
13        wood.BrickColor = BrickColor.new("Pine Cone")
14        tool.Parent = player.Backpack
15        script.Parent:Destroy() -- Destroy the tree
16    end
17end)

Another issue: You had a local function but you never even triggered it. You need to trigger functions in order for them to work, otherwise they will just be floating around and not doing much.

Ad

Answer this question