Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
CjayPlyz 643 Moderation Voter
4 years ago
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.

0
note : line 19 - 21 causes the error CjayPlyz 643 — 4y
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. DeceptiveCaster 3761 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

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
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)

0
This answer is not correct. DeceptiveCaster 3761 — 4y
0
ok snowwyyyyy2 30 — 4y

Answer this question