Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my script give a "stack overflow" error?

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.

2 comments

0

note : line 19 - 21 causes the error

0

Well, it is a bit obvious. You're calling the function in an if statement that will always be true, which causes the script to endlessly run the function since it always passes the if statement.

2 answers

✓ Accepted answer

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)

2 comments

0

This answer is not correct.

0

ok

This discussion is preserved as it was. New answers, comments, and votes are closed.