Stopping actions from happening if tool is unequipped

Hey there, I want to stop certain things from happening once a tool is unequipped.

local newSound = Instance.new("Sound")
local debounce = false

script.Parent.Activated:Connect(function()
	if debounce == false then
		debounce = true
		local SoundService = game:GetService("SoundService")
		newSound.SoundId = "rbxassetid://9117286784"
		SoundService:PlayLocalSound(newSound)
		wait(7.2)
		newSound:Destroy()
		game.Players.LocalPlayer.leaderstats.Notes.Value = game.Players.LocalPlayer.leaderstats.Notes.Value + 1
		debounce = false
	end
end)

This is a simple script and within the script I want to:

  • Stop the audio
  • Make sure the player doesn’t recieve any Notes if the tool has been unequipped

Any help would be appreciated.

If you want to remove the sounds and notes from showing then you should just connect a function to unequipping the tool.
Heres an example:

tool.Unequipped:Connect(function()
    newSound:Destroy()
    -- or whatever else you want to do
end)

Do you know how I would go about not giving the player any Notes as it will give the player the stats anyways?

I’m not sure how your game works, so I don’t quite understand what you mean.

So, when a tool is activated, the player recieves a point (Note). I don’t know how to stop that if the tool gets unequipped.

Do you get a point when the player clicks with the tool, or do you just get one point for every time that the player equips the tool?

Everytime the player clicks the tool.

Then, there shouldn’t be any issue. If you are using Tool.Activated, it won’t give you Notes for clicking unless the tool is equipped.

You need to read the script I provided. The whole event ends after 7.2 seconds of the tool being activated. I don’t know how to make it so if the tool is unequipped mid-way, it would not give the player a point.

Oh, sorry for the misunderstanding. I would probably have a variable used to check that the player is holding the tool and then check that when you give them the Note. Like this:

local ToolEquipped = false

script.Parent.Equipped:Connect(function()
    ToolEquipped = true
end)

script.Parent.Unequipped:Connect(function()
    ToolEquipped = false
end)

local newSound = Instance.new("Sound")
local debounce = false

script.Parent.Activated:Connect(function()
	if debounce == false then
		debounce = true
		local SoundService = game:GetService("SoundService")
		newSound.SoundId = "rbxassetid://9117286784"
		SoundService:PlayLocalSound(newSound)
		wait(7.2)
		newSound:Destroy()
        if ToolEquipped then
		    game.Players.LocalPlayer.leaderstats.Notes.Value = game.Players.LocalPlayer.leaderstats.Notes.Value + 1
        end    
		debounce = false
	end
end)

This way, the Note will not be rewarded if the tool has been unequipped

1 Like

Greatly appreciated, thank you!