I have a simple gun that destroys a certain part when clicked on said part. Here's the very basic script, without the code to destroy the part:
local enabled = true function Down(mouse) -- code to perform the task of destroying certain parts goes here end function Selected(mouse) local mouse = game.Players.LocalPlayer:GetMouse() mouse.Icon = "http://www.roblox.com/asset/?id=212765652" local l = game.Players.LocalPlayer.PlayerGui.bow.Frame:findFirstChild("slideValue") mouse.Button1Down:connect(function() Down(mouse) end) end script.Parent.Equipped:connect(Selected) function unSelected(mouse) local l = game.Players.LocalPlayer.PlayerGui.bow.Frame:findFirstChild("slideValue") local mouse = game.Players.LocalPlayer:GetMouse() mouse.Icon = "" end script.Parent.Unequipped:connect(unSelected)
The problem is that even when the tool is unequipped, it still fires. When there isn't code to change the mouse's icon, the tool doesn't do this, so that is the main problem here. Why is it doing this?
EDIT: A very easy way to solve this would to check if the tool is equipped and THEN play the script. Unfortunately, I don't know how I would check if the tool is equipped in an "if then" statement. So, I'll need help with that.
Well there are 2 ways that I know of that solve this problem. The first method is the easy method, the second is the slightly harder method.
Method 1: if then statement
In order to do this, you have to define a variable. You can name it whatever you want, but for the sake of the example, I'm going to name it "Selected"
Essentially, what you have to do is make the script set the Selected variable to true
whenever you select the gun, and false
whenever you deselect the gun. Then in the events that are supposed to run only when the gun is selected, you put an if then statement before it that only is true if the variable is true, like so:
local Selected = false --It's set to false because the gun's original state is deselected function onMouseButton1Down(mouse) --Code end function onEquipped(mouse) Selected = true --Sets the selected variable to true mouse.Button1Down:connect(function() if Selected then --This only lets the below function active if the gun is selected onMouseButton1Down(mouse) end end) end function onUnEquipped(mouse) Selected = false --Sets the selected variable to false end Tool.Equipped:connect(onEquipped) Tool.Unequipped:connect(onUnEquipped)
Method 2: :disconnect()
This method is a little more advanced, but what it essentially does is that it disconnects the event when you deselect the tool. I use this method because it's a more secure way of making sure events don't fire after the gun is deselected. Basically, you assign the event to a variable when you select the gun, and then when you deselect the gun, you use the :disconnect()
method on that variable to disconnect the event, which basically disconnects whatever function you connected to the event.
local Event = nil --Nil because it's not a number, string, or bool value function onMouseButton1Down(mouse) --Code end function onEquipped(mouse) Event = mouse.Button1Down:connect(function() --This sets the Event variable as the event onMouseButton1Down(mouse) end) end function onUnEquipped(mouse) Event:disconnect() --This disconnects the connected function from the event Event = nil --For safety end Tool.Equipped:connect(onEquipped) Tool.Unequipped:connect(onUnEquipped)
There might be more ways to do it, but these are the 2 I know of. Hope this helped!