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

I was making a module to make my in-game UI easier to build guis but I got this error when I was testing it: attempt to index nil with 'CreateWindow'
The code:
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
I was searching about this error with a function but I couldn't find any so I'm trying if here I can get any help about this error because I have been working very hard on this to make my GUI easier to build but well it's now struggling me to find any solutions.

The error is not in the provided code.
It must be something where you do.
someVariable["CreateWindow"], someVariable.CreateWindow or someVariable:CreateWindow() where someVariable is a nil value. Indexing nil values is not allowed.
You failed to refer that variable to your FrameLib table. Maybe just a typo? Lua errors typically come with a stack trace that provides you with information about which line of which file cause the error together with the sequence of function calls that lead to that error.
If you don't know the three possible indexing operations in Lua and hence what to look for in this case you probably should revisit the basics of Lua befor doing any further Roblox development.

Related

Problem showing how many studs of leght is for a part

I got a problem while testing a billboard GUI that shows how many studs of length there are for the part but it doesn't show the studs it just stays zero I tried testing with a loop but didn't work then tried with RunService but still didn't work so I got really confused because it doesn't show any error in the output so please help
The code:
function setBill(text, parent)
local Loop = {}
local BillboardGui = Instance.new("BillboardGui")
local ScreenGui = Instance.new("ScreenGui")
local Frame = Instance.new("Frame")
local TextLabel = Instance.new("TextLabel")
BillboardGui.Parent = parent
BillboardGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
BillboardGui.Active = true
BillboardGui.AlwaysOnTop = true
BillboardGui.LightInfluence = 1.000
BillboardGui.Size = UDim2.new(0, 205, 0, 55)
BillboardGui.ResetOnSpawn = false
ScreenGui.Parent = BillboardGui
ScreenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
Frame.Parent = BillboardGui
Frame.BackgroundColor3 = Color3.fromRGB(71, 71, 71)
Frame.BackgroundTransparency = 1
Frame.BorderColor3 = Color3.fromRGB(255, 255, 255)
Frame.BorderSizePixel = 5
Frame.Size = UDim2.new(0, 150, 0, 50)
TextLabel.Parent = Frame
TextLabel.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
TextLabel.BackgroundTransparency = 1.000
TextLabel.Size = UDim2.new(0, 150, 0, 50)
TextLabel.Font = Enum.Font.Gotham
TextLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
TextLabel.TextScaled = false
TextLabel.TextSize = 24.000
TextLabel.TextWrapped = true
local function BillLoop()
while wait() do
local pos = math.floor((BillboardGui.Parent.Position - BillboardGui.Parent.Position).magnitude)
TextLabel.Text = text.." Studs : "..tostring(pos)
end
end
Loop = game:GetService("RunService").RenderStepped:Connect(BillLoop)
end
setBill("Shop", game.Workspace["test part"])
local pos = math.floor((BillboardGui.Parent.Position
- BillboardGui.Parent.Position).magnitude)
TextLabel.Text = text.." Studs : "..tostring(pos)
This is where you calculate and set the text for your TextLabel.
BillboardGui.Parent.Position is a Vector3.
BillboardGui.Parent.Position - BillboardGui.Parent.Position is (0,0,0)
The magnitude aka length of a zero vector is 0.
This is probably not what you intended to calculate.

What does this error mean? I cannot seem to get this code to work

Im trying to get this lua code to run but cannot seem to get it to work. Whenever i run it i get this error that i cant seem to avoid no matter how many times i edit it.
Can anybody find a solution for this? I have been searching all over the web to no avail.
This is the error i get
input:58: unexpected symbol near eof
Here is the code:
-- Made By Vincent
local ScreenGui = Instance.new("ScreenGui")
local Main = Instance.new("Frame")
local RoundedCorners = Instance.new("UICorner")
local BlackHoleGun = Instance.new("TextButton")
local UICorner = Instance.new("UICorner")
local GoldenPistol = Instance.new("TextButton")
local UICorner_2 = Instance.new("UICorner")
local CrazyFuturisticMinigun = Instance.new("TextButton")
local UICorner_3 = Instance.new("UICorner")
local BiochemicalBlaster = Instance.new("TextButton")
local UICorner_4 = Instance.new("UICorner")
local SpaceRifle = Instance.new("TextButton")
local UICorner_5 = Instance.new("UICorner")
local LavaPistol = Instance.new("TextButton")
local UICorner_6 = Instance.new("UICorner")
local Scar = Instance.new("TextButton")
local UICorner_7 = Instance.new("UICorner")
local RayGun = Instance.new("TextButton")
local UICorner_8 = Instance.new("UICorner")
local SniperRifle = Instance.new("TextButton")
local UICorner_9 = Instance.new("UICorner")
--Properties:
ScreenGui.Parent = game.CoreGui
Main.Name = "Main"
Main.Parent = ScreenGui
Main.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
Main.Position = UDim2.new(0.383399218, 0, 0.121621616, 0)
Main.Size = UDim2.new(0, 426, 0, 211)
Main.Active = true
Main.Draggable = true
RoundedCorners.Name = "RoundedCorners"
RoundedCorners.Parent = Main
BlackHoleGun.Name = "Black Hole Gun"
BlackHoleGun.Parent = ScreenGui
BlackHoleGun.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
BlackHoleGun.Position = UDim2.new(0.393280655, 0, 0.136486486, 0)
BlackHoleGun.Size = UDim2.new(0, 121, 0, 49)
BlackHoleGun.Font = Enum.Font.GothamBold
BlackHoleGun.Text = "Black Hole Gun"
BlackHoleGun.TextColor3 = Color3.fromRGB(255, 219, 0)
BlackHoleGun.TextScaled = true
BlackHoleGun.TextSize = 14.000
BlackHoleGun.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
BlackHoleGun.TextWrapped = true
BlackHoleGun.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "Black Hole Gun"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
UICorner.Parent = BlackHoleGun
GoldenPistol.Name = "Golden Pistol"
GoldenPistol.Parent = ScreenGui
GoldenPistol.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
GoldenPistol.Position = UDim2.new(0.533596873, 0, 0.136486486, 0)
GoldenPistol.Size = UDim2.new(0, 121, 0, 49)
GoldenPistol.Font = Enum.Font.GothamBold
GoldenPistol.Text = "Golden Pistol"
GoldenPistol.TextColor3 = Color3.fromRGB(255, 219, 0)
GoldenPistol.TextScaled = true
GoldenPistol.TextSize = 14.000
GoldenPistol.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
GoldenPistol.TextWrapped = true
NAMEOFBUTTONHERE.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "Golden Pistol"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
end)
UICorner_2.Parent = GoldenPistol
CrazyFuturisticMinigun.Name = "Crazy Futuristic Minigun"
CrazyFuturisticMinigun.Parent = ScreenGui
CrazyFuturisticMinigun.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
CrazyFuturisticMinigun.Position = UDim2.new(0.673913062, 0, 0.136486486, 0)
CrazyFuturisticMinigun.Size = UDim2.new(0, 121, 0, 49)
CrazyFuturisticMinigun.Font = Enum.Font.GothamBold
CrazyFuturisticMinigun.Text = "Crazy Futuristic Minigun"
CrazyFuturisticMinigun.TextColor3 = Color3.fromRGB(255, 219, 0)
CrazyFuturisticMinigun.TextScaled = true
CrazyFuturisticMinigun.TextSize = 14.000
CrazyFuturisticMinigun.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
CrazyFuturisticMinigun.TextWrapped = true
NAMEOFBUTTONHERE.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "Crazy Futuristic Minigun"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
end)
UICorner_3.Parent = CrazyFuturisticMinigun
BiochemicalBlaster.Name = "Biochemical Blaster"
BiochemicalBlaster.Parent = ScreenGui
BiochemicalBlaster.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
BiochemicalBlaster.Position = UDim2.new(0.393280655, 0, 0.231081083, 0)
BiochemicalBlaster.Size = UDim2.new(0, 121, 0, 49)
BiochemicalBlaster.Font = Enum.Font.GothamBold
BiochemicalBlaster.Text = "Biochemical Blaster"
BiochemicalBlaster.TextColor3 = Color3.fromRGB(255, 219, 0)
BiochemicalBlaster.TextScaled = true
BiochemicalBlaster.TextSize = 14.000
BiochemicalBlaster.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
BiochemicalBlaster.TextWrapped = true
NAMEOFBUTTONHERE.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "Biochemical Blaster"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
end)
UICorner_4.Parent = BiochemicalBlaster
SpaceRifle.Name = "Space Rifle"
SpaceRifle.Parent = ScreenGui
SpaceRifle.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
SpaceRifle.Position = UDim2.new(0.533596873, 0, 0.231081083, 0)
SpaceRifle.Size = UDim2.new(0, 121, 0, 49)
SpaceRifle.Font = Enum.Font.GothamBold
SpaceRifle.Text = "Space Rifle"
SpaceRifle.TextColor3 = Color3.fromRGB(255, 219, 0)
SpaceRifle.TextScaled = true
SpaceRifle.TextSize = 14.000
SpaceRifle.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
SpaceRifle.TextWrapped = true
NAMEOFBUTTONHERE.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "Space Rifle"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
end)
UICorner_5.Parent = SpaceRifle
LavaPistol.Name = "Lava Pistol"
LavaPistol.Parent = ScreenGui
LavaPistol.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
LavaPistol.Position = UDim2.new(0.672924936, 0, 0.231081083, 0)
LavaPistol.Size = UDim2.new(0, 121, 0, 49)
LavaPistol.Font = Enum.Font.GothamBold
LavaPistol.Text = "Lava Pistol"
LavaPistol.TextColor3 = Color3.fromRGB(255, 219, 0)
LavaPistol.TextScaled = true
LavaPistol.TextSize = 14.000
LavaPistol.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
LavaPistol.TextWrapped = true
NAMEOFBUTTONHERE.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "Lava Pistol"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
end)
UICorner_6.Parent = LavaPistol
Scar.Name = "Scar"
Scar.Parent = ScreenGui
Scar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
Scar.Position = UDim2.new(0.392292529, 0, 0.322972983, 0)
Scar.Size = UDim2.new(0, 121, 0, 49)
Scar.Font = Enum.Font.GothamBold
Scar.Text = "Scar"
Scar.TextColor3 = Color3.fromRGB(255, 219, 0)
Scar.TextScaled = true
Scar.TextSize = 14.000
Scar.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
Scar.TextWrapped = true
NAMEOFBUTTONHERE.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "SCAR"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
end)
UICorner_7.Parent = Scar
RayGun.Name = "Ray Gun"
RayGun.Parent = ScreenGui
RayGun.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
RayGun.Position = UDim2.new(0.533596873, 0, 0.322972983, 0)
RayGun.Size = UDim2.new(0, 121, 0, 49)
RayGun.Font = Enum.Font.GothamBold
RayGun.Text = "Ray Gun"
RayGun.TextColor3 = Color3.fromRGB(255, 219, 0)
RayGun.TextScaled = true
RayGun.TextSize = 14.000
RayGun.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
RayGun.TextWrapped = true
NAMEOFBUTTONHERE.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "Ray Gun"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
end)
UICorner_8.Parent = RayGun
SniperRifle.Name = "Sniper Rifle"
SniperRifle.Parent = ScreenGui
SniperRifle.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
SniperRifle.Position = UDim2.new(0.672924936, 0, 0.322972983, 0)
SniperRifle.Size = UDim2.new(0, 121, 0, 49)
SniperRifle.Font = Enum.Font.GothamBold
SniperRifle.Text = "Sniper Rifle"
SniperRifle.TextColor3 = Color3.fromRGB(255, 219, 0)
SniperRifle.TextScaled = true
SniperRifle.TextSize = 14.000
SniperRifle.TextStrokeColor3 = Color3.fromRGB(174, 38, 148)
SniperRifle.TextWrapped = true
NAMEOFBUTTONHERE.MouseButton1Down:connect(function()
local Remote = game.Workspace.__THINGS.__REMOTES['weaponequipped']
local Arguments = {
[1] = {
[1] = {
[1] = "Sniper Rifle"
},
[2] = {
[1] = false
}
}
}
Remote:FireServer(unpack(Arguments))
end)
UICorner_9.Parent = SniperRifle
With time you'll learn how to find such errors quickly. If you have no idea where to start, there are several ways to approach this.
read the code and in your mind check what is missing ( pro level )
delete everything from your code that is syntactically correct
in your example you'll end up with BlackHoleGun.MouseButton1Down:connect(function() which is an incomplete function call with an imcomplete function definition. So you know that you have to put an end at the end of that function's body and that you need to a ) to close that function call.
count things that come in pairs (most editors can do that).
in your example you have 36{, 36}, 54[, 54], 105(, 104) whoops! -> 1 ) short!
you have 8 ends for 0 do + 0 then + 9 function -> 1 end short!
now check where those things are missing and add them.
A very good way to avoid such errors is to have your text editor/ide autocomplete pairs.
You're missing the end of the function BlackHoleGun.MouseButton1Down:connect you are declaring in line 53. There is no closing bracket.
I think you are missing
end)
after line 67.

Strange LUA Error While Scripting Garry's Mod SWEP

I am currently working on a Garry's Mod SWEP for my friends server. I have been troubleshooting for about 8 hours, when i got this strange error:
line 13: attempt to index global 'SWEP' (a nil value)
stack traceback:
t.lua:13: in main chunk
[C]: ?
not sure what it means, as i am fairly new to LUA.
Here is my whole script (also at codepad):
if SERVER then
SWEP.Weight = 30
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = true
end
if CLIENT then
SWEP.PrintName = "Bill Nye's Test Weapon"
SWEP.Slot = 4
SWEP.SlotPos = 1
end
SWEP.Base = "weapon_base" -- line 13
SWEP.Category = "Bill Nye's Admin Weapons"
SWEP.Author = "Bill Nye The Russian Spy"
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.ViewModelFlip = true
SWEP.HoldType = "ar2"
SWEP.ViewModel =
{["element_name2"] = { type = "Model", model = "models/XQM/cylinderx2.mdl", bone = "v_weapon.AK47_Parent", rel = "", pos = Vector(0.027, 4.118, 0.518), angle = Angle(-90.025, -4.764, -0.364), size = Vector(0.167, 0.167, 0.167), color = Color(90, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} },
["element_name4"] = { type = "Model", model = "models/props_lab/huladoll.mdl", bone = "v_weapon.AK47_Parent", rel = "", pos = Vector(-0.89, 3.947, 1.452), angle = Angle(-86.752, -1.621, -180), size = Vector(0.273, 0.273, 0.273), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} },
["element_name3"] = { type = "Model", model = "models/props_phx/construct/glass/glass_plate1x1.mdl", bone = "v_weapon.AK47_Parent", rel = "", pos = Vector(0.017, 6.109, 2.431), angle = Angle(0, 0, 0), size = Vector(0.041, 0.041, 0.041), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 3, bodygroup = {} },
["element_name"] = { type = "Model", model = "models/sprops/misc/alphanum/alphanum_plu.mdl", bone = "v_weapon.AK47_Parent", rel = "", pos = Vector(0.067, 6.19, 2.417), angle = Angle(0, -1.17, 87.662), size = Vector(0.107, 0.107, 0.107), color = Color(255, 0, 0, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {}}
}
SWEP.WorldModel =
{["element_name2"] = { type = "Model", model = "models/XQM/cylinderx2.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(4.316, 0.736, -4.292), angle = Angle(0, 0, 0), size = Vector(0.167, 0.167, 0.167), color = Color(90, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} },
["element_name4"] = { type = "Model", model = "models/props_lab/huladoll.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(4.987, 0.634, -5.249), angle = Angle(180, 17.653, 0), size = Vector(0.273, 0.273, 0.273), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} },
["element_name3"] = { type = "Model", model = "models/props_phx/construct/glass/glass_plate1x1.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(12.293, 0.656, -7.63), angle = Angle(88.204, 1.24, 1.22), size = Vector(0.041, 0.041, 0.041), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 3, bodygroup = {} },
["element_name"] = { type = "Model", model = "models/sprops/misc/alphanum/alphanum_plu.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(12.619, 0.646, -7.801), angle = Angle(0, 89.783, 0.244), size = Vector(0.107, 0.107, 0.107), color = Color(255, 0, 0, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {}}
}
SWEP.Primary.Recoil = 0.00
SWEP.Primary.Damage = 1000
SWEP.Primary.NumShots = 1
SWEP.Primary.Spread = 0
SWEP.Primary.Cone = 0
SWEP.Primary.ClipSize = 27
SWEP.Primary.DefaultClip = 100
SWEP.Primary.Ammo = "ar2"
SWEP.Primary.Automatic = true
SWEP.Primary.Delay = 0.1
SWEP.IronSightsPos = Vector(6.099, -13.468, 2.612)
SWEP.IronSightsAng = Vector(3.517, 0, -0)
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = "none"
function SWEP:Initialize()
self:SetWeaponHoldType(self.HoldType)
end
function SWEP:PrimaryAttack()
if (self:CanPrimaryAttack()) then
return
end
local Bullet = {}
Bullet.Num = self.Primary.NumShots
Bullet.Src = self.Owner:GetShootPos ()
Bullet.Dir = self.Owner:GetAimVector ()
Bullet.Spread = Vector(self.Primary.Spread, self.Primary.Spread,0)
Bullet.Tracer = 0
Bullet.Damage = self.Primary.Damage
Bullet.AmmoType = self.Primary.Ammo
self:ShootEffects()
self:FireBullets(Bullet)
self:EmitSound ("Weapon_IRifle.Single")
self:TakePrimaryAmmo ( 1 )
self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
end
function SWEP:SecondaryAttack()
return false
end
It means:
in your t.lua file on line 13 you point to SWEP value, which is a null value.
Did you call: SWEP:initialize()?
function SWEP:Initialize()
self:SetWeaponHoldType(self.HoldType)
end
check if your value is "local" or if your SWEP value has been initialized.
also check if you remove it before like this: SWEP = nil or not.

How to insert rows into TableView in Corona?

I'm trying to render the match results (string.find) in to a Row, with kinda works... but its only displaying the last match. so if I match 'jan' and 'kevin' it will; only list 'kevin'
Is there a way to fix this ?
code:
local MasterData = xml:loadFile( "sample.xml")
local XMLdataTEST = {}
for i=1,#MasterData.child do
XMLdataTEST[i] = MasterData.child[i]
end
inputNumber = 1
check1 = 'jan'
check2 = 'kevin'
for i=1,#XMLdataTEST do
local data1 = XMLdata[i].child[1].value
local data2 = XMLdata[i].child[2].value
local data3 = XMLdata[i].child[3].value
local data4 = XMLdata[i].child[4].value
input1 = string.lower( data1.. "" )
input2 = string.lower(_G['check' .. inputNumber] )
input = input2
if string.find( input1.. "" , input ) then
print(inputNumber.. " match with " ..input)
inputNumber = inputNumber + 1
local function onRowRender( event )
local row = event.row
local number = display.newText( row, "" .. row.index .. " - ", 12, 0, nil, 18 )
number:setReferencePoint( display.CenterLeftReferencePoint )
number.x = 15
number.y = row.height * 0.5
number:setFillColor( 0, 0, 0 )
local name = display.newText(row, input1, 12, 0, nil, 18 )
name:setReferencePoint( display.CenterLeftReferencePoint )
name.x = number.x + number.contentWidth
name.y = row.height * 0.5
name:setFillColor( 0, 0, 0 )
local score = display.newText(row,"testy", 12, 0, nil, 18 )
score:setReferencePoint( display.CenterLeftReferencePoint )
score.x = display.contentWidth - score.contentWidth - 20
score.y = row.height * 0.5
score:setFillColor( 0, 0, 0 )
end
local tableView = widget.newTableView
{
left = 0,
top = 0,
height = display.contentHeight,
width = display.contentWidth,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
tableView.x = display.contentWidth + display.contentWidth/2 + 50
transition.to( tableView, { time=500, x=display.contentWidth / 2, transition=easing.inOutExpo } )
for i = 1, 1 do
local isCategory = false
local rowHeight = 40
local rowColor = { 255, 255, 255 }
local lineColor = { 220, 220, 220 }
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
onRender = onRowRender,
}
end
end
end
Your tableView being declared within the outer loop meant that the first instance of it would be transitioned over by the second instance. And a quick look over the docs indicates that each event.row allows an optional params table to include any data you may need to render the row.
local MasterData = xml:loadFile( "sample.xml")
local XMLdataTEST = {}
for i=1,#MasterData.child do
XMLdataTEST[i] = MasterData.child[i]
end
-- ** moved from loop **
local function onRowRender( event )
local row = event.row
local number = display.newText( row, "" .. row.index .. " - ", 12, 0, nil, 18 )
number:setReferencePoint( display.CenterLeftReferencePoint )
number.x = 15
number.y = row.height * 0.5
number:setFillColor( 0, 0, 0 )
-- ** changed to use params table **
local name = display.newText(row, row.params.input1, 12, 0, nil, 18 )
name:setReferencePoint( display.CenterLeftReferencePoint )
name.x = number.x + number.contentWidth
name.y = row.height * 0.5
name:setFillColor( 0, 0, 0 )
local score = display.newText(row,"testy", 12, 0, nil, 18 )
score:setReferencePoint( display.CenterLeftReferencePoint )
score.x = display.contentWidth - score.contentWidth - 20
score.y = row.height * 0.5
score:setFillColor( 0, 0, 0 )
end
-- ** moved from loop **
local tableView = widget.newTableView {
left = 0,
top = 0,
height = display.contentHeight,
width = display.contentWidth,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
tableView.x = display.contentWidth + display.contentWidth/2 + 50
transition.to( tableView, { time=500, x=display.contentWidth / 2, transition=easing.inOutExpo } )
inputNumber = 1
check1 = 'jan'
check2 = 'kevin'
for i=1,#XMLdataTEST do
local data1 = XMLdata[i].child[1].value
local data2 = XMLdata[i].child[2].value
local data3 = XMLdata[i].child[3].value
local data4 = XMLdata[i].child[4].value
input1 = string.lower( data1.. "" )
input2 = string.lower(_G['check' .. inputNumber] )
input = input2
if string.find( input1.. "" , input ) then
print(inputNumber.. " match with " ..input)
inputNumber = inputNumber + 1
local isCategory = false
local rowHeight = 40
local rowColor = { 255, 255, 255 }
local lineColor = { 220, 220, 220 }
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
-- ** pass input1 to onRowRender **
params = { input1 = input1 }
}
end
end

Corona SDK: Error: "attempt to index field "?" a nil value", but only when I am on a certain scene

So I am making a game which has a lot of values to save. I used a module by Rob Miracle with makes it easy to save and load tables to do this with two tables. One is named "gamestuff" and the other is named "leveltries".
In one scene I declare the values of these tables (only if they haven't been saved before)
gamestuff.level = 1
gamestuff.sound = true
gamestuff.name = "user"
gamestuff.highscore = 0
gamestuff.lastscore = 0
gamestuff.volume = 0.5
gamestuff.lastlevel = 1
gamestuff.won = false
gamestuff.bouncesleft = 0
local leveltries = {
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
}
loadsave.saveTable(leveltries, "leveltries.json")
loadsave.saveTable(gamestuff, "gamestuff.json")
This works fine except for when I try to go to a restart level scene and a win scene after the first level.
But it confuses me because in level one I try
print("Fails: "..leveltries[gamestuff.lastlevel].fails)
and it works
But when I try it in the restarter scene, it doesn't:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local loadsave = require("loadsave")
local gamestuff = {}
local leveltries = {}
local options =
{
effect = "fade",
time = 400
}
local function loader()
gamestuff = loadsave.loadTable("gamestuff.json")
leveltries = loadsave.loadTable("leveltries.json")
print("loaded")
end
local function save()
loadsave.saveTable(gamestuff, "gamestuff.json")
loadsave.saveTable(leveltries, "leveltries.json")
end
function scene:createScene(event)
gamestuff = {}
leveltries = {}
end
function scene:enterScene(event)
loader()
print("Fails"..leveltries[gamestuff.lastlevel].fails) --says the error is here
local a = leveltries[gamestuff.lastlevel].fails + 1
leveltries[gamestuff.lastlevel].fails = a
timer.performWithDelay( 50, function()
save()
storyboard.gotoScene("level"..tostring(gamestuff.lastlevel), options)
end, 1 )
end
function scene:exitScene(event)
local group = self.view
storyboard.removeScene("restarter")
end
function scene:destroyScene(event)
gamestuff = nil
leveltries = nil
options = nil
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
Can anyone tell my why the code above doesn't work?
Could it be that I don't properly remove the "gamestuff" and "leveltries" in level1?
I tried to put some prints in. In the level 1 scene:
Everything works:
print(gamestuff.lastlevel)
print(leveltries[gamestuff.lastlevel].fails)
print(leveltries[gamestuff.lastlevel])
However in the restarter scene, only:
print(gamestuff.lastlevel) --which prints 1 and is right
works
print(leveltries[gamestuff.lastlevel]), in the restarter scene prints nil
I don't know how this is possible. I load the same tables in the same way at the same time in both scenes and used them in the same way.
Could it be that I am removing the tables incorrectly?
In scene one these are my exitScene and removeScene:
function scene:exitScene(event)
Runtime:removeEventListener("touch", shoot)
menub:removeEventListener("touch", gotomenu)
restartb:removeEventListener("touch", restart)
goal:removeEventListener("collision", win)
ball:removeEventListener("collision", hit)
ball:removeEventListener("collision", grow)
Runtime:removeEventListener("enterFrame", twinkle)
Runtime:removeEventListener("enterFrame", redraw)
end
function scene:didExitScene(event)
--storyboard.removeScene("level1")
end
function scene:destroyScene(event)
bg:removeSelf()
ball:removeSelf()
goal:removeSelf()
topwall:removeSelf()
bottomwall:removeSelf()
rightwall:removeSelf()
leftwall:removeSelf()
menub:removeSelf()
bounces = nil
bouncestext:removeSelf()
taps = nil
restartb:removeSelf()
tuttext:removeSelf()
gamestuff = nil
bg = nil
ball = nil
goal = nil
topwall = nil
bottomwall = nil
rightwall = nil
leftwall = nil
menub = nil
bouncestext = nil
restartb = nil
tuttext = nil
options = nil
trailprops = nil
for i = 1, 3 do
wallColor[i] = nil
end
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("didExitScene", scene)
scene:addEventListener("destroyScene", scene)
I think the issue is , you are declaring the gamestuff and leveltries array at the beginning of the scene. Then y do u again declare it in the create scene. You are again overriding the same array. I think it doesnt make any sense. Remove the array which is declared in the create scene and try .

Resources