Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why am I getting an error of nil when I click the GUI button to switch?

So here is my script,

local GUI = script.Parent
local Button = GUI:WaitForChild("Open/Close")
local TButton = Button.Frame.Frame["N/A"]

local State = "Closed"
local DB = false
Button.MouseButton1Down:Connect(function()
	if DB == false then
		DB = true
		spawn(function()
			if State == "Closed" then
				State = "Opened"
				Button.Frame.Frame:TweenPosition(UDim2.new(0,0,0,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quint,1,true)
			elseif State == "Opened" then
				State = "Closed"
				Button.Frame.Frame:TweenPosition(UDim2.new(0,0,-1,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quint,1,true)
			end
		end)
		wait(.6)
		DB = false
	end
end)

local Teams = {
	[1]={
		Label="Class-D"
	},
	[2]={
		Label="Foundation Personnel"
	},
	[3]={
		Label="Security Department"
	},
	[4]={
		Label="Mobile Task Forces"
	},
	[5]={
		Label="Department of External Affairs"
	},
	[6]={
		Label="Engineering and Technical"
	},
	[7]={
		Label="Ethics Committee"
	},
	[8]={
		Label="Intelligence Agency"
	},
	[9]={
		Label="Internal Security Department"
	},
	[10]={
		Label="Medical Department"
	},
	[11]={
		Label="Rapid Response Team"
	},
	[12]={
		Label="Scientific Department"
	},
}


local tindex = Teams[0]
for _,B in pairs(Button.Frame.Frame:GetChildren()) do
	if B:IsA("TextButton") then
		B.MouseButton1Down:Connect(function()
			if B.Name == "Up" then
				TButton.Name = tindex + 1 .. ".Label"
				TButton.Text = tindex + 1 .. ".Label"
			elseif B.Name == "Down" then
				TButton.Name = tindex - 1 .. ".Label"
				TButton.Text = tindex + 1 .. ".Label"
			else
				game.ReplicatedStorage.updateTeam:FireServer(B.Name)
			end
		end)
	end
end

It is a team script that should change your teams with the GUI but it does not..

And I am getting the error of:

Players.bt6k.PlayerGui.TeamGUI.LocalScript:72: attempt to perform arithmetic (sub) on nil and number

I am quite new to lua, and was wondering what might cause it?

1 answer

Hello.


Explaining the Error:

The error: "attempt to perform arithmetic (sub) on nil and number" means that you are trying to use math on something that doesn't exist (nil) and a number.


Solution:

Things like table[0] or [table[-1] do not exist because a table's index begins at 1. That was exactly what you were doing. I belive you accidentally used "-" instead of "+" so I've fixed that.


Fixed Script:

local GUI = script.Parent
local Button = GUI:WaitForChild("Open/Close")
local TButton = Button.Frame.Frame["N/A"]

local State = "Closed"
local DB = false
Button.MouseButton1Down:Connect(function()
    if DB == false then
        DB = true
        spawn(function()
            if State == "Closed" then
                State = "Opened"
                Button.Frame.Frame:TweenPosition(UDim2.new(0,0,0,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quint,1,true)
            elseif State == "Opened" then
                State = "Closed"
                Button.Frame.Frame:TweenPosition(UDim2.new(0,0,-1,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quint,1,true)
            end
        end)
        wait(.6)
        DB = false
    end
end)

local Teams = {
    [1]={
        Label="Class-D"
    },
    [2]={
        Label="Foundation Personnel"
    },
    [3]={
        Label="Security Department"
    },
    [4]={
        Label="Mobile Task Forces"
    },
    [5]={
        Label="Department of External Affairs"
    },
    [6]={
        Label="Engineering and Technical"
    },
    [7]={
        Label="Ethics Committee"
    },
    [8]={
        Label="Intelligence Agency"
    },
    [9]={
        Label="Internal Security Department"
    },
    [10]={
        Label="Medical Department"
    },
    [11]={
        Label="Rapid Response Team"
    },
    [12]={
        Label="Scientific Department"
    },
}


local tindex = Teams[0]
for _,B in pairs(Button.Frame.Frame:GetChildren()) do
    if B:IsA("TextButton") then
        B.MouseButton1Down:Connect(function()
            if B.Name == "Up" then
                TButton.Name = tindex + 1 .. ".Label"
                TButton.Text = tindex + 1 .. ".Label"
            elseif B.Name == "Down" then
                TButton.Name = tindex + 1 .. ".Label"
                TButton.Text = tindex + 1 .. ".Label"
            else
                game.ReplicatedStorage.updateTeam:FireServer(B.Name)
            end
        end)
    end
end

Please accept my answer if it helps. There's been a bug going around where you can't accept answers for newer users. If that happens, try going onto incognito mode.

4 comments

0

Does not seem to be working...

0

Does it still error?

0

Yes. Same exact thing.

0

i do not know what is wrong.

This discussion is preserved as it was. New answers, comments, and votes are closed.