So, the script is supposed to shoot out a ball that does 10 damage, grows, etc. IT works just fine. That is until you unequip the tool and requip it. The output says "The Mouse Is No Longer Active", I can't wrap my head around how to fix this other than making the script reset all variables after it is unequipped.
local tool = script.Parent tool.Equipped:connect(function(mouse) local player = game.Players.LocalPlayer oh = player.Character.Torso.Position game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.E then local blhl = Instance.new("Part", workspace) blhl.Name = "blhl" blhl.CFrame = CFrame.new(oh) blhl.Shape = 0 blhl.BrickColor = BrickColor.new(1003) blhl.CanCollide = false local bp = Instance.new("BodyPosition", blhl) bp.Position = Vector3.new(mouse.Hit.x,mouse.Hit.y,mouse.Hit.z) bp.MaxForce = Vector3.new(10000, 10000, 10000) bp.D = 30000 bp.P = 100000 local function ontouch(hit) if workspace:findFirstChild("blhl") then if hit.Parent:findFirstChild 'Humanoid' then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10 blhl:Destroy() end end end blhl.Touched:connect(ontouch) for i = .5, 5, .1 do local h = blhl.CFrame blhl.Size = Vector3.new(i,i,i) blhl.CFrame = (h) wait() end for i = 5, 10, .1 do local h = blhl.CFrame blhl.Size = Vector3.new(i,i,i) blhl.CFrame = (h) wait() end for i = 1, 0, .1 do blhl.Transparency = i wait() blhl:Destroy() end while oh ~= player.Character.Torso.Position do oh = player.Character.Torso.Position end end end) end)
You are going to need to disconnect the events that are being created when the tool is equipped.
In order to call :disconnect()
, we need to have the RBXScriptSignal that is created when you called :connect()
. Lucky, the connect call returns back this very object. All we have to do is store it in a variable and disconnect when the time is right (i.e. the tool is unequipped):
local UserInput = game:GetService("UserInputService") local tool = script.Parent local connection tool.Equipped:connect(function(mouse) connection = UserInput.InputBegan:connect(function(inputObject, gameProcessed) -- code end) end) tool.Unequipped:connect(function(mouse) if connection then connection:disconnect() end end)
However, you don't have to disconnect your touched event because the method :Destroy()
automatically does that for you.