#script timeout issue

1 messages · Page 1 of 1 (latest)

lavish quarry
#

i have a script that is only sometimes timing out, and i cant figure out why.

my code is a tree randomization script, so only when certain numbers are generated the code stops working

this is a segment of my code, which is in a server script in ServerScriptService

local branch_locations = {}

local function create_branch1()
    local branch_width = math.random(trunk_width*55,trunk_width*75)/100

    local distance = math.random(trunk_height*50, trunk_height*75)/100

    local offset_x = math.random(-50,50)/10
    local offset_z = math.random(-50,50)/10

    local offset_y = distance*2 - (math.abs(offset_x) + math.abs(offset_z))

    if offset_y < 2 then
        offset_y += math.random(20,30)/10
    end

    local initial_pos = (trunk.CFrame * CFrame.new(0,trunk.Size.Y/2,0)).Position
    local final_pos = initial_pos + Vector3.new(offset_x,offset_y,offset_z)

    local distance = (final_pos - initial_pos).Magnitude

    local bridge_part = Instance.new("Part")
    bridge_part.Size = Vector3.new(branch_width, branch_width, distance)
    bridge_part.Position = Vector3.new(
        (initial_pos.X + final_pos.X)/2,
        (initial_pos.Y + final_pos.Y)/2,
        (initial_pos.Z + final_pos.Z)/2)

    if not (#branch_locations == 0) then
        for i, pos in ipairs(branch_locations) do
            if (final_pos - pos).Magnitude > 6 then continue end --this line here seems to be the issue
            create_branch1(trunk, trunk_width, trunk_height)
            continue
        end
    end

    table.insert(branch_locations, final_pos)

    bridge_part.Name = "BridgePart"
    bridge_part.CFrame = CFrame.lookAt(bridge_part.Position, final_pos)
    bridge_part.Anchored = true
    bridge_part.BrickColor = BrickColor.new("Brown")
    bridge_part.Name = "Branch"
    bridge_part.Parent = workspace

    print(branch_width.. " 2")
    return bridge_part, branch_width, distance
end
#

this is the error message btw

ServerScriptService.CreateTree:45: Script timeout: exhausted allowed execution time
mild oracle
#

add a task.wait(0.003)