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
5 years ago
01local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")
02local Mouse = game.Players.LocalPlayer:GetMouse()
03local Tool = script.Parent
04local IsActivated
05 
06local function OnActivated ()
07    local Target = Mouse.Target
08    if Target then
09        local Durability = Target:FindFirstChild("Durability")
10        if Durability then
11            IsActivated = true
12            while IsActivated == true and Target.Parent ~= nil and Durability.Value > 0 do
13                wait(0.1)
14                Durability.Value = Durability.Value - 1
15            end
View all 33 lines...

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 — 5y
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 — 5y

2 answers

Log in to vote
1
Answered by 5 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

1local function OnActivated ()
2    while IsActivated do
3        --code in OnActivated
4    end
5end

Note: If you want to take advantage of tail call optimizations in Lua, return the return value of the function, eg:

1local function foo()
2    return foo()
3end
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 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.

01local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")
02local Mouse = game.Players.LocalPlayer:GetMouse()
03local Tool = script.Parent
04 
05local IsActivated = false
06 
07local function OnActivated ()
08 
09    local Target = Mouse.Target
10 
11    if Target then
12 
13        local Durability = Target:FindFirstChild("Durability")
14 
15        if Durability then
View all 38 lines...

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 — 5y
0
ok snowwyyyyy2 30 — 5y

Answer this question