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

How do I place a tool on click instead of having to touch it?

Asked by 4 years ago

So I have this script where if you have a tool out in your hand, and you touch the brick that this script is inside of, it places the tool that is named in the script. In this specific one it is named Sandbag. I want to make it so you click instead of touch.

local Part = script.Parent
local debounce = false
local SandbagString = "Block"

Part.Touched:Connect(function(toucher)
    if toucher.Parent:FindFirstChild(SandbagString) and toucher.Parent:FindFirstChild("Humanoid") and debounce == false then
        debounce = true
        local char = toucher.Parent
        local Sandbag = char[SandbagString]
        local Handle = Sandbag.Handle
        Handle.Parent = Part
        Sandbag:Destroy()
        Handle.Anchored = true
        Handle.CanCollide = false
        Handle.Position = Part.Position+Vector3.new(0,.8,0)
        Handle.Orientation = Vector3.new(0,math.random(0,0),0)
    end
end)

Thank you!

1 answer

Log in to vote
0
Answered by 4 years ago

You just want to put a ClickDetector in the brick, and hook up a MouseClick event to that function you've already written.

local clickDetector = Part:WaitForChild("ClickDetector")

clickDetector.MouseClick:Connect(function(player)
    -- all your sandbag stuff
end)

P.S: When it comes to debounce, it's conventional for your bool variable to start as true, and check if debounce then as opposed to if not debounce then

local debounce = true
local debounceWait = 5 -- seconds
clickDetector.MouseClick:Connect(function(player)
    if debounce then
        debounce = false
        -- all your sandbag stuff
        wait(debounceWait)
        debounce = true
    end
end)
Ad

Answer this question