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

How to make an event run when you click a part with a certain tool?

Asked by 4 years ago

Hello, can someone tell me how to make a part that when it has been clicked with an particular item equipped it will run an event,

Thanks

0
u have a click detecter do clickdetecter.MouseClick:Connect(function(player) then check if the character has a tool HappyTimIsHim 652 — 4y

3 answers

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

Ok so this isnt hard but you could make a script in each teach doing this

local part = game.Workspace.PARTNAME
local handle = script.Parent.Handle

part.Touched:Connect(function(hit)
if handle hit then

--RUN FUNCTION HERE
end)
0
well i stated when a part has been clicked not touched InfiniteBlackFlame 2 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Hey there! Making this is pretty simple. I'll show you how!

Make sure to have a ClickDetector inserted in your part. This will help detect when the part has been clicked.

Code:

local Part = game.Workspace.PartName --reference variable for the part, make sure to change PartName to your part's name
local ClickDetector = Part.ClickDetector --reference variable for the clickdetector inside of your part
local Tool = script.Parent --reference variable for the tool

Tool.Equipped:Connect(function() --event for whenever the tool is equipped, meaning that the code in this function will only run when the tool is equipped
    ClickDetector.MouseClick:Connect(function() --event for when the part is clicked, and it will only run when the tool is equipped, since of that other event
        --Function here
    end) 
end)

That should work, and hopefully you understood that. Have a great day.

0
It did not work unfortunately because when i equip the tool the click detector on the part disappears. InfiniteBlackFlame 2 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I think i have an answer for your question, so basically you are trying to click the part only if you have an specific tool equipped right? It's not that hard, just disable the script, as you can see in the script properties you will see a "Disabled" property, click that to disable the script so it wont work. And now you need to enable it when the tool is equipped so now scripts locations. Make a script inside the tool and a script inside the clickdetector of the part you want to click. And now to be able to click it only when the tool is equipped go to the script of the tool and do this:

local isEquipped = false
local tool = script.Parent
local script = workspace.Part.ClickDetector.Script -- Change it if it's wrong.

tool.Equipped:Connect(function()
if not isEquipped then
isEquipped = true
script.Disabled = false
end
end)

tool.Unequipped:Connect(function()
if not isEquipped then
isEquipped = false
script.Disabled = true
end
end)

That would be the code you must do to enable the script when the specific tool is equipped. I really hope this answer helps you and good luck with it! If you have any errors in the output fix them and if something isn't working, remember to use Print() after the functions to see if the functions are functioning haha! Also what happened to the script within the ClickDetector? Idk you need to put your own code there haha. Make sure you Disable the script of the ClickDetector and not the tool's script one! Also big sorry to don't use codeblock, i'm not pretty good using codeblocks here, but the important is that you can see how to do it. Byee :)

Answer this question