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

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

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

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)

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