Script Exhausts Execution Time

i am trying to make basic nodes for dialogue that can execute functions after the dialogue finishes, (practical use cases include shops, making enemies attack you after you finish talking to them, etc.) but when i tried to make it, all it did was provide the dreaded “script timeout” error. i don’t know why this is happening, and any help would be appreciated.

-- id table
local idTable = {
    "Node1",
    "Node2",
    "Node3",
}

-- dialogue nodes
local dialogueNodes = {
    Node1 = {
        text = "this is node 1.",
        nextNode = "node2",
        action = function()
            print("executing action for node 1.")
        end
    },
    Node2 = {
        text = "this is node 2.",
        nextNode = "node3",
        action = function()
            print("executing action for node 2.")
        end
    },
    Node3 = {
        text = "this is node 3.",
        nextNode = "node1",  -- loop back to node1 for demonstration purposes
        action = function()
            print("executing action for node 3.")
        end
    },
}

local function startDialogue(startingNode)
    local currentNode = dialogueNodes[startingNode]

    while currentNode do
        print(currentNode.text)

        if currentNode.action then
            currentNode.action()
        end

        currentNode = dialogueNodes[currentNode.nextNode]
    end
end

startDialogue("node1")
2 Likes

You have to add a task.wait() to the loop.

5 Likes

the reason this is happening is because the script is repeatedly running over and over too fast for the computer to handle, because it doesn’t know to wait a couple frames before it runs the loop again. SilentSuprion’s solution is correct, btw.

1 Like