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

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

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.

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 = Instance.new("Tool")
	tool.Name = "Pine"
local handle = Instance.new("Part")
	handle.Parent = tool
	handle.Material = Enum.Material.Wood
	handle.BrickColor = BrickColor.new("Pine Cone")
	

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

(go easy on me with the obvious answers)

1 comment

0

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

1 answer

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

clickDetector.MouseClick:Connect(function(player)
	clickLimit = clickLimit - 1
	if clickLimit <= 0 then
		local tool = Instance.new("Tool") -- Create a new tool
		tool.Name = "Pine"
		local wood = Instance.new("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 = BrickColor.new("Pine Cone")
		tool.Parent = player.Backpack
		script.Parent:Destroy() -- Destroy the tree
	end
end)

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.

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