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

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

Asked by 2 months 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 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.

local pines = game.Workspace.Game.Forests.PinePoint --Pine Point is the name of the forest and Forests is the folder.

local pine = pines:FindFirstChild(pines.PineTree)
local tool ="Tool")
    tool.Name = "Pine"
local handle ="Part")
    handle.Parent = tool
    handle.Material = Enum.Material.Wood
    handle.BrickColor ="Pine Cone")

    local function giveTool(player, tool)
        local backpack = player:FindFirstChildOfClass("Backpack")
        if backpack then
            tool.Parent = backpack

(go easy on me with the obvious answers)

I don't understand why you'd be defining a function inside of an anonymous function, but it's your script. ParticularlyPenguin 69 — 2mo

1 answer

Log in to vote
Answered by
Raccoonyz 691 Donator Moderation Voter
2 months ago
Edited 2 months 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.

local clickDetector = script.Parent.ClickDetector -- Assumes that the script is in a model which has the ClickDetector
local clickLimit = 5

    clickLimit = clickLimit - 1
    if clickLimit <= 0 then
        local tool ="Tool") -- Create a new tool
        tool.Name = "Pine"
        local wood ="Part") -- Create the wood / handle for the tool
        wood.Parent = tool
        wood.Name = "Handle" -- Name the wood "Handle" to make sure it is the handle of the tool
        wood.Material = Enum.Material.Wood
        wood.BrickColor ="Pine Cone")
        tool.Parent = player.Backpack
        script.Parent:Destroy() -- Destroy the tree

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.


Answer this question