local RemoteEvent = script.Parent:WaitForChild("RemoteEvent") local Mouse = game.Players.LocalPlayer:GetMouse() local Tool = script.Parent local IsActivated local function OnActivated () local Target = Mouse.Target if Target then local Durability = Target:FindFirstChild("Durability") if Durability then IsActivated = true while IsActivated == true and Target.Parent ~= nil and Durability.Value > 0 do wait(0.1) Durability.Value = Durability.Value - 1 end if IsActivated == true and Target.Parent ~= nil and Durability.Value == 0 then RemoteEvent:FireServer(Target, Target.Value.Value) -- focus if IsActivated == true then OnActivated() end -- focus end end end end local function OnDeactivated () IsActivated = false end Tool.Activated:Connect(OnActivated) Tool.Deactivated:Connect(OnDeactivated)
I don't know why this happen's but i'm pretty sure its because of the OnActivated()
but im not sure, it should only run twice but it repeats a lot.
Concept : When everything goes right, it makes it so that the function would run again.
Specific : When everything goes as it is expected (IsActivated == true and Target.Parent ~= nil and Durability.Value == 0
) then it would "run" the function over again.
In OnActivated, you use recursion, which either never ends or repeats a lot. Since Roblox disabled tail call optimizations, you should consider using a loop instead of using recursion.
In your case, it seems like you're using recursion simply to repeat code with no new arguments passed. In this case, you'd just need a loop in the function like
local function OnActivated () while IsActivated do --code in OnActivated end end
Note: If you want to take advantage of tail call optimizations in Lua, return the return value of the function, eg:
local function foo() return foo() end
You accidently fit a if statement that defines a function that the if statements defines You are making a loop of that function since its inside it.
local RemoteEvent = script.Parent:WaitForChild("RemoteEvent") local Mouse = game.Players.LocalPlayer:GetMouse() local Tool = script.Parent local IsActivated = false local function OnActivated () local Target = Mouse.Target if Target then local Durability = Target:FindFirstChild("Durability") if Durability then IsActivated = true while IsActivated == true and Target.Parent ~= nil and Durability.Value > 0 do wait(0.1) Durability.Value = Durability.Value - 1 end end end end if IsActivated == true and Target.Parent ~= nil and Durability.Value == 0 then RemoteEvent:FireServer(Target, Target.Value.Value) -- focus if IsActivated == true then OnActivated() end -- focus end local function OnDeactivated () IsActivated = false end Tool.Activated:Connect(OnActivated) Tool.Deactivated:Connect(OnDeactivated)
You also set IsActivated to nothing, you declared the variable, but with no value. Which is an error universally for all programming languages. (Except for HTML or CSS, they aint programming languages)