How to make something happen when you click on screen on a tool

Hello,

I’m currently making a tool that when you click your mouse a sound plays, does anyone know how to make this happen, I know this is pretty obvious, I’m just new to scripting.

2 Likes

Here is an article to help you with that, make sure you fully understand how to use sound in a game.

Article Link: Sound | Documentation - Roblox Creator Hub

if you are looking for tool events and how to script with them, check out this article as well:

P.S a good tip when making tool is to set the “CanBeDropped” setting off so people can’t drop tools in your game when clicking “backspace”. Hope This Helped! :grin:

2 Likes

Alright, I understand that, but it hasn’t got anything to do with when your click your mouse button.

You can use the Tool.Activated event that fires when the tool is equipped and the mouse is clicked.

Tool.Activated:Connect(function() -- connect function to Tool.Activated event
	Sound:Play() -- play sound
end)

All I get is a Players.AviaSkip.Backpack.Tool.Sound.Script:1: attempt to index nil with ‘Activated’

I’ve set it out like this, correct? Screenshot by Lightshot

You need to set Tool as a variable.

    local Tool = script.Parent

Still doesn’t work “Activated is not a valid member of Sound”

My bad, I didn’t realize the script was in the sound instead of the tool. I suggest parenting the script to the tool itself to make further changes to the tool a little simpler.

local tool = script.Parent
local sound = tool.Sound
tool.Activated:Connect(function()
    sound:Play()
end)
1 Like

Thank you so much, much appreciated!

3 Likes

In the future, please search first and attempt these kinds of problems yourself before asking. If you’re unfamiliar with scripting, check out the abundance of threads regarding learning scripting or the Developer Hub to kickstart your journey.

A lot of your questions could easily be answered by reading the documentation pages that @Hello42bacon provided you. There are various methods that detail what they do and how you’re able to use them. You can then use the knowledge you gain to make something.

The goal here is to make something happen when you click with a tool. Therefore, you need a tool and a way to check for clicks. Insert the tool, done. Get a script (preferably a LocalScript in this scenario) to program the tool, done. Now you need a way to make it do something on activation. Look to Tool.Activate, which is clearly described as “simulates a click on a tool”. Connect to this signal with a function. Any code you want running when the tool’s activated, put it in that function. Done.

2 Likes