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

Trying to Instance a tool, can't seem to figure it out. Can anyone help?

I'm new to the whole development aspect of Roblox and I've been working on improving my skills as a rookie developer. I've been working on a game involving cutting down trees to get wood and I wanted to make it so that the wood you just collected by chopping down a tree is in your inventory and you're able to hold it. This means that it will have to be a tool and is needed to be a child of the StarterPack.

I've tried various different things and I've come to a conclusion that Instancing a tool would be the best way to go (just because it's going to be a block of wood and should be easy to model via code) I saw on the web that a tool needs to have a child named "Handle" to be able to be held so, here's my code:

local pines = game.Workspace.Game.LumberForest.LumberPines
 -- LumberForest and LumberPines are the folders, LumberForest is the forest and LumberPines is a folder full of Pine Trees

local pine = pines:FindFirstChild(pines.PineTree)

script.Parent.MouseClick:Connect(function()
	
	game.Workspace.Game.LumberForest.LumberPines.PineTree:Destroy()
	
	local wood = Instance.new("Tool")
		wood.Parent = game.StarterPack
		wood.Name = "Pine"
	
	local handle = Instance.new("Part")
		handle.Parent = wood
		handle.Name = "Handle"
		handle.Material = "Wood"	

end)

I've been debugging this for quite a while and I still can't seem to figure it out. If there's an easy solution to this, please let me know (and go easy on me, I'm new)

1 comment

0

What exactly is the problem? Also, you should set the parent property last ( https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296 )

1 answer

Hello there.

Problem:

You set the tool's parent to StarterPack.

Solution:

Set it to the player's backpack so the player receives it using the parameter of ClickDetector.MouseClick.

Recommendations:

  1. Use Enum's instead of a string for the material.

  2. Set the handle's parent to the tool and then set the tool's parent to the player's backpack.

Fixed Code:

local pines = game.Workspace.Game.LumberForest.LumberPines
 -- LumberForest and LumberPines are the folders, LumberForest is the forest and LumberPines is a folder full of Pine Trees

local pine = pines:FindFirstChild(pines.PineTree)

script.Parent.MouseClick:Connect(function(player)

    game.Workspace.Game.LumberForest.LumberPines.PineTree:Destroy()

    local wood = Instance.new("Tool")
	wood.Name = "Pine"

    local handle = Instance.new("Part")
	handle.Name = "Handle"
        handle.Material = Enum.Material.Wood   
	handle.Parent = wood

	wood.Parent = player.Backpack
end)

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