我如何修复在 roblox 中使用函数索引 nil 的尝试?

How do i fix attemp to index nil with function in roblox?

我正在制作一个模块,使我的游戏中 UI 更容易构建图形用户界面,但我在测试它时遇到了这个错误:尝试使用 'CreateWindow' 索引 nil 代码:

local FrameLib = {}

local TweenService = game:GetService("TweenService")

function FrameLib:CreateWindow(WindowName)
    local UICornerMain = Instance.new("UICorner")
    local UIListLayout = Instance.new("UIListLayout")
    local cvb3 = Instance.new("ScreenGui")
    local Main = Instance.new("Frame")
    local Top = Instance.new("Frame")
    local UICorner_2 = Instance.new("UICorner")
    local Bottom = Instance.new("Frame")
    local Close = Instance.new("TextButton")
    local UICorner_3 = Instance.new("UICorner")
    local Shadow = Instance.new("Folder")
    local UmbraShadow = Instance.new("ImageLabel")
    local PenumbraShadow = Instance.new("ImageLabel")
    local AmbientShadow = Instance.new("ImageLabel")
    local UICorner_4 = Instance.new("UICorner")
    local Minimize = Instance.new("TextButton")
    local UICorner_5 = Instance.new("UICorner")
    local Shadow_2 = Instance.new("Folder")
    local UmbraShadow_2 = Instance.new("ImageLabel")
    local PenumbraShadow_2 = Instance.new("ImageLabel")
    local AmbientShadow_2 = Instance.new("ImageLabel")
    local TextLabel = Instance.new("TextLabel")
    local Pattern = Instance.new("ImageLabel")
    local AmbientShadow1 = Instance.new("ImageLabel")
    local UIAspectRatioConstraint = Instance.new("UIAspectRatioConstraint")
    local AmbientShadow2 = Instance.new("ImageLabel")
    local UIAspectRatioConstraint_2 = Instance.new("UIAspectRatioConstraint")
    local PenumbraShadow1 = Instance.new("ImageLabel")
    local UIAspectRatioConstraint_3 = Instance.new("UIAspectRatioConstraint")
    local PenumbraShadow2 = Instance.new("ImageLabel")
    local UIAspectRatioConstraint_4 = Instance.new("UIAspectRatioConstraint")
    local UmbraShadow1 = Instance.new("ImageLabel")
    local UIAspectRatioConstraint_5 = Instance.new("UIAspectRatioConstraint")
    local UmbraShadow2 = Instance.new("ImageLabel")
    local UIAspectRatioConstraint_6 = Instance.new("UIAspectRatioConstraint")
    local Stroke = Instance.new("UIStroke")
    local ButtonFrame = Instance.new("ScrollingFrame")
    local Sections = Instance.new("Frame")
    cvb3.Name = "Main"
    cvb3.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
    cvb3.ZIndexBehavior = Enum.ZIndexBehavior.Sibling

    Main.Name = "Main"
    Main.Parent = cvb3
    Main.AnchorPoint = Vector2.new(0, 0.5)
    Main.BackgroundColor3 = Color3.fromRGB(63, 63, 63)
    Main.BorderSizePixel = 0
    Main.ClipsDescendants = true
    Main.Position = UDim2.new(0.374725521, 0, 0.478996277, 0)
    Main.Size = UDim2.new(0, 421, 0, 300)

    UICornerMain.CornerRadius = UDim.new(0, 5)
    UICornerMain.Parent = Main

    Stroke.Name = "Stroke"
    Stroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Contextual
    Stroke.Color = Color3.fromRGB(149,149,149)
    Stroke.LineJoinMode = Enum.LineJoinMode.Round
    Stroke.Thickness = 1
    Stroke.Enabled = true
    Stroke.Parent = Main

    Top.Name = "Top"
    Top.Parent = Main
    Top.BackgroundColor3 = Color3.fromRGB(44, 44, 44)
    Top.BorderSizePixel = 0
    Top.Size = UDim2.new(0, 421, 0, 32)

    UICorner_2.CornerRadius = UDim.new(0, 5)
    UICorner_2.Parent = Top

    Bottom.Name = "Bottom"
    Bottom.Parent = Top
    Bottom.BackgroundColor3 = Color3.fromRGB(44, 44, 44)
    Bottom.BorderSizePixel = 0
    Bottom.Position = UDim2.new(0, 0, 0.736249983, 0)
    Bottom.Size = UDim2.new(0, 421, 0, 8)

    Close.Name = "Close"
    Close.Parent = Top
    Close.BackgroundColor3 = Color3.fromRGB(38, 38, 38)
    Close.BorderSizePixel = 0
    Close.Position = UDim2.new(0.914489329, 0, 0.0625, 0)
    Close.Selectable = false
    Close.Size = UDim2.new(0, 28, 0, 29)
    Close.ZIndex = 2
    Close.Selected = true
    Close.Font = Enum.Font.GothamSemibold
    Close.Text = "X"
    Close.TextColor3 = Color3.fromRGB(255, 255, 255)
    Close.TextSize = 15.000
    Close.TextWrapped = true

    UICorner_3.CornerRadius = UDim.new(0, 5)
    UICorner_3.Parent = Close

    Shadow.Name = "Shadow"
    Shadow.Parent = Close

    UmbraShadow.Name = "UmbraShadow"
    UmbraShadow.Parent = Shadow
    UmbraShadow.AnchorPoint = Vector2.new(0.5, 0.5)
    UmbraShadow.BackgroundTransparency = 1.000
    UmbraShadow.BorderSizePixel = 0
    UmbraShadow.Position = UDim2.new(0.5, 0, 0.5, 6)
    UmbraShadow.Size = UDim2.new(1, 10, 1, 10)
    UmbraShadow.Image = "rbxassetid://1316045217"
    UmbraShadow.ImageColor3 = Color3.fromRGB(0, 0, 0)
    UmbraShadow.ImageTransparency = 0.860
    UmbraShadow.ScaleType = Enum.ScaleType.Slice
    UmbraShadow.SliceCenter = Rect.new(10, 10, 118, 118)

    PenumbraShadow.Name = "PenumbraShadow"
    PenumbraShadow.Parent = Shadow
    PenumbraShadow.AnchorPoint = Vector2.new(0.5, 0.5)
    PenumbraShadow.BackgroundTransparency = 1.000
    PenumbraShadow.BorderSizePixel = 0
    PenumbraShadow.Position = UDim2.new(0.5, 0, 0.5, 1)
    PenumbraShadow.Size = UDim2.new(1, 18, 1, 18)
    PenumbraShadow.Image = "rbxassetid://1316045217"
    PenumbraShadow.ImageColor3 = Color3.fromRGB(0, 0, 0)
    PenumbraShadow.ImageTransparency = 0.880
    PenumbraShadow.ScaleType = Enum.ScaleType.Slice
    PenumbraShadow.SliceCenter = Rect.new(10, 10, 118, 118)

    AmbientShadow.Name = "AmbientShadow"
    AmbientShadow.Parent = Shadow
    AmbientShadow.AnchorPoint = Vector2.new(0.5, 0.5)
    AmbientShadow.BackgroundTransparency = 1.000
    AmbientShadow.BorderSizePixel = 0
    AmbientShadow.Position = UDim2.new(0.5, 0, 0.5, 3)
    AmbientShadow.Size = UDim2.new(1, 5, 1, 5)
    AmbientShadow.Image = "rbxassetid://1316045217"
    AmbientShadow.ImageColor3 = Color3.fromRGB(0, 0, 0)
    AmbientShadow.ImageTransparency = 0.800
    AmbientShadow.ScaleType = Enum.ScaleType.Slice
    AmbientShadow.SliceCenter = Rect.new(10, 10, 118, 118)

    UICorner_4.CornerRadius = UDim.new(0, 5)
    UICorner_4.Parent = Top

    Minimize.Name = "Minimize"
    Minimize.Parent = Top
    Minimize.BackgroundColor3 = Color3.fromRGB(38, 38, 38)
    Minimize.BorderSizePixel = 0
    Minimize.Position = UDim2.new(0.831353962, 0, 0.0625, 0)
    Minimize.Size = UDim2.new(0, 28, 0, 29)
    Minimize.ZIndex = 2
    Minimize.Font = Enum.Font.GothamSemibold
    Minimize.Text = "-"
    Minimize.TextColor3 = Color3.fromRGB(255, 255, 255)
    Minimize.TextSize = 15.000
    Minimize.TextWrapped = true

    UICorner_5.CornerRadius = UDim.new(0, 5)
    UICorner_5.Parent = Minimize

    Shadow_2.Name = "Shadow"
    Shadow_2.Parent = Minimize

    UmbraShadow_2.Name = "UmbraShadow"
    UmbraShadow_2.Parent = Shadow_2
    UmbraShadow_2.AnchorPoint = Vector2.new(0.5, 0.5)
    UmbraShadow_2.BackgroundTransparency = 1.000
    UmbraShadow_2.BorderSizePixel = 0
    UmbraShadow_2.Position = UDim2.new(0.5, 0, 0.5, 6)
    UmbraShadow_2.Size = UDim2.new(1, 10, 1, 10)
    UmbraShadow_2.Image = "rbxassetid://1316045217"
    UmbraShadow_2.ImageColor3 = Color3.fromRGB(0, 0, 0)
    UmbraShadow_2.ImageTransparency = 0.860
    UmbraShadow_2.ScaleType = Enum.ScaleType.Slice
    UmbraShadow_2.SliceCenter = Rect.new(10, 10, 118, 118)

    PenumbraShadow_2.Name = "PenumbraShadow"
    PenumbraShadow_2.Parent = Shadow_2
    PenumbraShadow_2.AnchorPoint = Vector2.new(0.5, 0.5)
    PenumbraShadow_2.BackgroundTransparency = 1.000
    PenumbraShadow_2.BorderSizePixel = 0
    PenumbraShadow_2.Position = UDim2.new(0.5, 0, 0.5, 1)
    PenumbraShadow_2.Size = UDim2.new(1, 18, 1, 18)
    PenumbraShadow_2.Image = "rbxassetid://1316045217"
    PenumbraShadow_2.ImageColor3 = Color3.fromRGB(0, 0, 0)
    PenumbraShadow_2.ImageTransparency = 0.880
    PenumbraShadow_2.ScaleType = Enum.ScaleType.Slice
    PenumbraShadow_2.SliceCenter = Rect.new(10, 10, 118, 118)

    AmbientShadow_2.Name = "AmbientShadow"
    AmbientShadow_2.Parent = Shadow_2
    AmbientShadow_2.AnchorPoint = Vector2.new(0.5, 0.5)
    AmbientShadow_2.BackgroundTransparency = 1.000
    AmbientShadow_2.BorderSizePixel = 0
    AmbientShadow_2.Position = UDim2.new(0.5, 0, 0.5, 3)
    AmbientShadow_2.Size = UDim2.new(1, 5, 1, 5)
    AmbientShadow_2.Image = "rbxassetid://1316045217"
    AmbientShadow_2.ImageColor3 = Color3.fromRGB(0, 0, 0)
    AmbientShadow_2.ImageTransparency = 0.800
    AmbientShadow_2.ScaleType = Enum.ScaleType.Slice
    AmbientShadow_2.SliceCenter = Rect.new(10, 10, 118, 118)

    TextLabel.Parent = Top
    TextLabel.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    TextLabel.BackgroundTransparency = 1.000
    TextLabel.Position = UDim2.new(0.0190023743, 0, 0.1875, 0)
    TextLabel.Size = UDim2.new(0, 200, 0, 21)
    TextLabel.Font = Enum.Font.GothamSemibold
    TextLabel.Text = WindowName
    TextLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
    TextLabel.TextScaled = true
    TextLabel.TextSize = 14.000
    TextLabel.TextWrapped = true
    TextLabel.TextXAlignment = Enum.TextXAlignment.Left
    TextLabel.TextYAlignment = Enum.TextYAlignment.Top

    Pattern.Name = "Pattern"
    Pattern.Parent = Main
    Pattern.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    Pattern.BackgroundTransparency = 1.000
    Pattern.Position = UDim2.new(-0.124703087, 0, -0.146666661, 0)
    Pattern.Size = UDim2.new(0, 504, 0, 372)
    Pattern.ZIndex = 0
    Pattern.Image = "rbxassetid://2151741365"
    Pattern.ImageTransparency = 0.400
    Pattern.ScaleType = Enum.ScaleType.Tile
    Pattern.SliceCenter = Rect.new(0, 256, 0, 256)
    Pattern.TileSize = UDim2.new(0, 250, 0, 250)

    AmbientShadow1.Name = "AmbientShadow1"
    AmbientShadow1.Parent = cvb3
    AmbientShadow1.AnchorPoint = Vector2.new(0.5, 0.5)
    AmbientShadow1.BackgroundTransparency = 1.000
    AmbientShadow1.BorderSizePixel = 0
    AmbientShadow1.Position = UDim2.new(0.509783506, 0, 0.481963903, 3)
    AmbientShadow1.Size = UDim2.new(0, 430, 0, 310)
    AmbientShadow1.ZIndex = 0
    AmbientShadow1.Image = "rbxassetid://1316045217"
    AmbientShadow1.ImageColor3 = Color3.fromRGB(0, 0, 0)
    AmbientShadow1.ImageTransparency = 0.800
    AmbientShadow1.ScaleType = Enum.ScaleType.Slice
    AmbientShadow1.SliceCenter = Rect.new(10, 10, 118, 118)

    UIAspectRatioConstraint.Parent = AmbientShadow1
    UIAspectRatioConstraint.AspectRatio = 1.386

    AmbientShadow2.Name = "AmbientShadow2"
    AmbientShadow2.Parent = cvb3
    AmbientShadow2.AnchorPoint = Vector2.new(0.5, 0.5)
    AmbientShadow2.BackgroundTransparency = 1.000
    AmbientShadow2.BorderSizePixel = 0
    AmbientShadow2.Position = UDim2.new(0.509783506, 0, 0.481963903, 3)
    AmbientShadow2.Size = UDim2.new(0, 430, 0, 310)
    AmbientShadow2.ZIndex = 0
    AmbientShadow2.Image = "rbxassetid://1316045217"
    AmbientShadow2.ImageColor3 = Color3.fromRGB(0, 0, 0)
    AmbientShadow2.ImageTransparency = 0.800
    AmbientShadow2.ScaleType = Enum.ScaleType.Slice
    AmbientShadow2.SliceCenter = Rect.new(10, 10, 118, 118)

    UIAspectRatioConstraint_2.Parent = AmbientShadow2
    UIAspectRatioConstraint_2.AspectRatio = 1.386

    PenumbraShadow1.Name = "PenumbraShadow1"
    PenumbraShadow1.Parent = cvb3
    PenumbraShadow1.AnchorPoint = Vector2.new(0.5, 0.5)
    PenumbraShadow1.BackgroundTransparency = 1.000
    PenumbraShadow1.BorderSizePixel = 0
    PenumbraShadow1.Position = UDim2.new(0.509274423, 0, 0.48696661, 1)
    PenumbraShadow1.Size = UDim2.new(0, 431, 0, 309)
    PenumbraShadow1.ZIndex = 0
    PenumbraShadow1.Image = "rbxassetid://1316045217"
    PenumbraShadow1.ImageColor3 = Color3.fromRGB(0, 0, 0)
    PenumbraShadow1.ImageTransparency = 0.880
    PenumbraShadow1.ScaleType = Enum.ScaleType.Slice
    PenumbraShadow1.SliceCenter = Rect.new(10, 10, 118, 118)

    UIAspectRatioConstraint_3.Parent = PenumbraShadow1
    UIAspectRatioConstraint_3.AspectRatio = 1.398

    PenumbraShadow2.Name = "PenumbraShadow2"
    PenumbraShadow2.Parent = cvb3
    PenumbraShadow2.AnchorPoint = Vector2.new(0.5, 0.5)
    PenumbraShadow2.BackgroundTransparency = 1.000
    PenumbraShadow2.BorderSizePixel = 0
    PenumbraShadow2.Position = UDim2.new(0.509274423, 0, 0.48696661, 1)
    PenumbraShadow2.Size = UDim2.new(0, 431, 0, 309)
    PenumbraShadow2.ZIndex = 0
    PenumbraShadow2.Image = "rbxassetid://1316045217"
    PenumbraShadow2.ImageColor3 = Color3.fromRGB(0, 0, 0)
    PenumbraShadow2.ImageTransparency = 0.880
    PenumbraShadow2.ScaleType = Enum.ScaleType.Slice
    PenumbraShadow2.SliceCenter = Rect.new(10, 10, 118, 118)

    UIAspectRatioConstraint_4.Parent = PenumbraShadow2
    UIAspectRatioConstraint_4.AspectRatio = 1.398

    UmbraShadow1.Name = "UmbraShadow1"
    UmbraShadow1.Parent = cvb3
    UmbraShadow1.AnchorPoint = Vector2.new(0.5, 0.5)
    UmbraShadow1.BackgroundTransparency = 1.000
    UmbraShadow1.BorderSizePixel = 0
    UmbraShadow1.Position = UDim2.new(0.509783506, 0, 0.478506416, 6)
    UmbraShadow1.Size = UDim2.new(0, 430, 0, 308)
    UmbraShadow1.ZIndex = 0
    UmbraShadow1.Image = "rbxassetid://1316045217"
    UmbraShadow1.ImageColor3 = Color3.fromRGB(0, 0, 0)
    UmbraShadow1.ImageTransparency = 0.860
    UmbraShadow1.ScaleType = Enum.ScaleType.Slice
    UmbraShadow1.SliceCenter = Rect.new(10, 10, 118, 118)

    UIAspectRatioConstraint_5.Parent = UmbraShadow1
    UIAspectRatioConstraint_5.AspectRatio = 1.395

    UmbraShadow2.Name = "UmbraShadow2"
    UmbraShadow2.Parent = cvb3
    UmbraShadow2.AnchorPoint = Vector2.new(0.5, 0.5)
    UmbraShadow2.BackgroundTransparency = 1.000
    UmbraShadow2.BorderSizePixel = 0
    UmbraShadow2.Position = UDim2.new(0.509783506, 0, 0.478506416, 6)
    UmbraShadow2.Size = UDim2.new(0, 430, 0, 308)
    UmbraShadow2.ZIndex = 0
    UmbraShadow2.Image = "rbxassetid://1316045217"
    UmbraShadow2.ImageColor3 = Color3.fromRGB(0, 0, 0)
    UmbraShadow2.ImageTransparency = 0.860
    UmbraShadow2.ScaleType = Enum.ScaleType.Slice
    UmbraShadow2.SliceCenter = Rect.new(10, 10, 118, 118)

    UIAspectRatioConstraint_6.Parent = UmbraShadow2
    UIAspectRatioConstraint_6.AspectRatio = 1.395

    ButtonFrame.Name = "ButtonFrame"
    ButtonFrame.Parent = Main
    ButtonFrame.Active = true
    ButtonFrame.BackgroundColor3 = Color3.fromRGB(44, 44, 44)
    ButtonFrame.BorderSizePixel = 0
    ButtonFrame.Position = UDim2.new(0, 0, 0.106666669, 0)
    ButtonFrame.Size = UDim2.new(0, 88, 0, 268)
    ButtonFrame.ScrollBarThickness = 0

    UIListLayout.HorizontalAlignment = Enum.HorizontalAlignment.Left
    UIListLayout.Parent = ButtonFrame

    AmbientShadow.Parent = Main
    AmbientShadow1.Parent = Main
    AmbientShadow2.Parent = Main
    PenumbraShadow.Parent = Main
    PenumbraShadow1.Parent = Main
    PenumbraShadow2.Parent = Main
    UmbraShadow.Parent = Main
    UmbraShadow1.Parent = Main
    UmbraShadow2.Parent = Main

    AmbientShadow.Position = UDim2.new(0.498, 0,0.506, 3)
    AmbientShadow1.Position = UDim2.new(0.498, 0,0.506, 3)
    AmbientShadow2.Position = UDim2.new(0.498, 0,0.506, 3)
    PenumbraShadow.Position = UDim2.new(0.498, 0,0.506, 3)
    PenumbraShadow1.Position = UDim2.new(0.498, 0,0.506, 3)
    PenumbraShadow2.Position = UDim2.new(0.498, 0,0.506, 3)
    UmbraShadow.Position = UDim2.new(0.498, 0,0.506, 3)
    UmbraShadow1.Position = UDim2.new(0.498, 0,0.506, 3)
    UmbraShadow2.Position = UDim2.new(0.498, 0,0.506, 3)

    AmbientShadow.ZIndex = -1
    AmbientShadow1.ZIndex = -1
    AmbientShadow2.ZIndex = -1
    PenumbraShadow.ZIndex = -1
    PenumbraShadow1.ZIndex = -1
    PenumbraShadow2.ZIndex = -1
    UmbraShadow.ZIndex = -1
    UmbraShadow1.ZIndex = -1
    UmbraShadow2.ZIndex = -1

    Sections.Name = "Sections"
    Sections.Parent = Main
    Sections.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    Sections.BackgroundTransparency = 1.000
    Sections.Position = UDim2.new(0.211401418, 0, 0.109999999, 0)
    Sections.Size = UDim2.new(0, 332, 0, 267)

    local PatStroke = Instance.new("UIStroke")
    local PatCorner = Instance.new("UICorner")
    PatCorner.Parent = Pattern
    PatCorner.CornerRadius = UDim.new(0,5)
    PatStroke.Parent = Pattern
    PatStroke.Color = Color3.fromRGB(149, 149, 149)
    PatStroke.LineJoinMode = Enum.LineJoinMode.Round
    Pattern.ZIndex = 0
    Pattern.BackgroundColor3 = Color3.fromRGB(63, 63, 63)
    Pattern.BackgroundTransparency = 0
end

我正在使用函数搜索此错误,但找不到任何错误,所以我正在尝试是否可以在这里获得有关此错误的任何帮助,因为我一直在努力解决这个问题以使我的 GUI 更容易构建,但现在我很难找到任何解决方案。

错误不在提供的代码中。

一定是你做的事情。

someVariable["CreateWindow"]someVariable.CreateWindowsomeVariable:CreateWindow() 其中 someVariable 是零值。不允许索引 nil 值。

您未能将该变量引用到您的 FrameLib table。也许只是一个错字? Lua 错误通常带有堆栈跟踪,为您提供有关哪个文件的哪一行导致错误以及导致该错误的函数调用序列的信息。

如果您不知道 Lua 中的三种可能的索引操作以及在这种情况下要查找的内容,您可能应该在进行任何进一步的 Roblox 开发之前重新访问 Lua 的基础知识。