I have this function i made and it works great but everytime i equip and unequip the tool it runs the function and itself all the times its been ran before???
kinda hard to explain
function Settings:ActivateTool()
local tool = script.Parent
local value = false
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local head = char:WaitForChild("Head")
local cam = game.Workspace.CurrentCamera
local Light = tool.Handle:WaitForChild("Light")
local Clone = Light:Clone()
Clone.Parent = workspace
Clone.WeldConstraint:Destroy()
Clone.CollisionGroup = "Viewmodel"
local connection =RunService.RenderStepped:Connect(function ()
Clone.CFrame = cam.CFrame
if tool.Parent ~= char then
Clone:Destroy()
return
end
end)
UIS.InputBegan:Connect(function (input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and value == false then
value = true
print("DOING")
-- PLEASE NOTE THIS PLAYS MORE AND MORE EVERYTIME THE FUNCTION IS CALLED
if Clone:FindFirstChild("SpotLight") then
Clone.SpotLight.Enabled = true
flashlightEvent:FireServer(true)
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
value = false
if Clone:FindFirstChild("SpotLight") then
Clone.SpotLight.Enabled = false
flashlightEvent:FireServer(false)
end
end
end)
tool.Unequipped:Connect(function ()
Clone:Destroy()
connection:Disconnect()
end)
flashlightEvent.OnClientEvent:Connect(function ()
tool.Handle.Light.SpotLight.Enabled = false
end)
end
You’re creating new event connections every time ActivateTool runs, but you’re not disconnecting all of them. Specifically: UIS.InputBegan:Connect(…) and flashlightEvent.OnClientEvent:Connect(…)
Remember to disconnect any events that are unused or unnecesarry. If you avoid doing this it can cause memory leaks to form, which just makes the game laggier.
the easiest solution i have to this is the exact same thing but a little different
EDIT: i did not do a great job reading and yeah you need to disconnect the UIS.InputBegan too
function Settings:ActivateTool()
local tool = script.Parent
local value = false
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local head = char:WaitForChild("Head")
local cam = game.Workspace.CurrentCamera
local Light = tool.Handle:WaitForChild("Light")
local Clone = Light:Clone()
Clone.Parent = workspace
Clone.WeldConstraint:Destroy()
Clone.CollisionGroup = "Viewmodel"
local connection --you can set this to nil if you want to
connection = RunService.RenderStepped:Connect(function()
Clone.CFrame = cam.CFrame
if tool.Parent ~= char then
Clone:Destroy()
return
end
end)
UIS.InputBegan:Connect(function (input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and value == false then
value = true
print("DOING")
-- PLEASE NOTE THIS PLAYS MORE AND MORE EVERYTIME THE FUNCTION IS CALLED
if Clone:FindFirstChild("SpotLight") then
Clone.SpotLight.Enabled = true
flashlightEvent:FireServer(true)
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
value = false
if Clone:FindFirstChild("SpotLight") then
Clone.SpotLight.Enabled = false
flashlightEvent:FireServer(false)
end
end
end)
tool.Unequipped:Connect(function ()
Clone:Destroy()
connection:Disconnect()
end)
flashlightEvent.OnClientEvent:Connect(function ()
tool.Handle.Light.SpotLight.Enabled = false
end)
end