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

How do I make a tool go onto a certain brick on click?

Asked by
ahlxs -8
5 years ago

So like, if I clicked a certain brick, the tool would go onto it, and then if I clicked it again, I would pick it back up again.

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You could do this with ClickDetectors.

For example, you could have a ClickDetector in the brick. There would be a script in that brick that would put the tool onto the brick, and a ClickDetector in the tool, and weld the tool to the brick, then detect when the tool is clicked and give it back to a player... Here, let me use an example:

local hasTool = false

local function setupTool(tool)
    hasTool = true
    tool.Parent = script.Parent

    local clickDetector = script.Parent.ClickDetector:Clone()
    clickDetector.Parent = tool

    local handle = tool:FindFirstChild('Handle')

    local toolWeld = Instance.new('Weld', tool.Handle)
    toolWeld.Part0 = handle
    toolWeld.Part1 = script.Parent
    toolWeld.C0 = CFrame.new(0, -1, 0)

    clickDetector.MouseClick:Connect(function(plr)
        local character = plr.Character or plr.CharacterAdded:Wait()
        local humanoid = character and character:FindFirstChildWhichIsA('Humanoid')
        local backpack = plr:FindFirstChild('Backpack')

        if not humanoid or not backpack then return end

        toolWeld:Destroy()
        clickDetector:Destroy()

        hasTool = false

        tool.Parent = backpack
        humanoid:EquipTool(tool)
    end)
end

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    if hasTool then return end

    local character = plr.Character

    if not character then return end

    local humanoid = character:FindFirstChildWhichIsA('Humanoid')
    local tool = character:FindFirstChildWhichIsA('Tool')
    local handle = tool and tool:FindFirstChild('Handle')

    if not humanoid or not handle then return end

    humanoid:UnequipTools()

    setupTool(tool)
end)

This is untested, I'd substitute the CFrame for C0 with something else, and it'll get messed up if you touch the tool to automatically pick it up again, but it could probably work.

Hope this helps!

0
Great answer! next time don't write a Script they can take and leave;) Ziffixture 6913 — 5y
0
Wait, how do I weld a tool to a brick? Sorry, I'm new to scripting. ahlxs -8 — 5y
0
Look in the code, around ToolWeld. That creates a weld between the part and the Handle, and sets the offset (C0). TerrodactyI 173 — 5y
0
@Feahren I intentionally don't test the scripts I post here TerrodactyI 173 — 5y
Ad

Answer this question