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

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

Asked by 4 years ago

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)

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 ) fpnky 0 — 4y

1 answer

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

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)
Ad

Answer this question