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

elseif problem i would like some help please? Your title should be specific?

First, I typed in a local script into a text button.

Here's the script:

LocalPlayer = game.Players.LocalPlayer
tableLots = script.Parent.Parent.Parent.TableLots

script.Parent.MouseButton1Click:connect(function()
    local text = script.Parent.Parent.TableHandler.Text
    local number = tonumber(text)
    print("Number ", number)

    if number >= 1 and number <= 18 then
        local chosenTable = tableLots["Table"..tostring(number)]
        print("Table ", chosenTable)
        chosenTable.BackgroundColor3 = Color3.fromRGB(255,0,0)
elseif
chosenTable.BackgroundColor3 == Color3.fromRGB(255,0,0) then
	script.Parent.Text = "Table already taken."
	wait(5)
	script.Parent.Text = "Please try again."
	wait(5)
	script.Parent.Text = "Submit"
    end
end

As I type in elseif and the continued script, nothing works.

It would be wonderful if someone explained the elseif part.

4 comments

0

Else if is written as "elseif" not "else if"

2

I tried that, didn't work, just a typo in the code block.

0

nigger

0

fuck you NIGGER

2 answers

✓ Accepted answer

chosenTable was out of scope (meaning the variable doesn't exist in the current context.)

Looking at the rest of your code, it seems like you're trying to alert the player that a table is taken.

If you were to put the code in an elseif, it would only run the code when the table number is either less than 1 or greater than 18, which isn't what we want to do.

We instead need to put the check to see if the table is empty inside the first if statement.

#The logic would go like this:

player inputs a number

if the number is between 1-18, the table number is valid then check if the valid table is taken, alert the player that it's taken and let them try again.

if the table is not not taken, give that player that table

if the number is greater than 18 or less than one, the player typed in an invalid table. we tell them that it is invalid and let them try again.

#Working Script:

LocalPlayer = game.Players.LocalPlayer
tableLots = script.Parent.Parent.Parent.TableLots

script.Parent.MouseButton1Click:Connect(function()
    local text = script.Parent.Parent.TableHandler.Text
    local number = tonumber(text)
    print("Number ", number)

    if number >= 1 and number <= 18 then
        local chosenTable = tableLots["Table"..tostring(number)]

		if(chosenTable.BackgroundColor3 == Color3.fromRGB(255,0,0))then
		   script.Parent.Text = "Table already taken."
		   wait(3)
		   script.Parent.Text = "Please try again."
		   wait(3)
		   script.Parent.Text = "Submit"
		else
	        chosenTable.BackgroundColor3 = Color3.fromRGB(255,0,0)
		end
	else
		script.Parent.Text = "There are only 18 tables.\n Please enter another table number."
	end
end)

There are a few things wrong.

-- these variables should be local because i'm assuming this isn't inside a function
local LocalPlayer = game.Players.LocalPlayer
local tableLots = script.Parent.Parent.Parent.TableLots

-- changed "connect" to "Connect", since "connect" is deprecated.
script.Parent.MouseButton1Click:Connect(function()
    local text = script.Parent.Parent.TableHandler.Text
    local number = tonumber(text)
    print("Number ", number)

    if number >= 1 and number <= 18 then
        local chosenTable = tableLots["Table"..tostring(number)]
        print("Table ", chosenTable)
        chosenTable.BackgroundColor3 = Color3.fromRGB(255,0,0)
-- elseif should be put together.
    elseif chosenTable.BackgroundColor3 == Color3.fromRGB(255,0,0) then
	    script.Parent.Text = "Table already taken."
	    wait(5)
	    script.Parent.Text = "Please try again."
	    wait(5)
	    script.Parent.Text = "Submit"
	    end
end)
-- You forgot the closed parenthesis

Haven't tested it, but fixed a lot of the formatting of this script. I'm assuming it's very old code. I also recommend using Activated instead of MouseButton1Click and Activated makes it compatible across all devices.

Accept this answer if it helped.

4 comments

0

Only the Text part didn't work, everything else is fine.

0

Stop begging for accepts.

1

Good job pointing out the end parenthesis. The else if does not need to be together. Logically, it works fine as else if. Having the variables at the top is fine as he might need to reference the local player in another function. It is nicer to have it defined at the top rather than having to define the local player in every function. tableLots could be a local variable, but how it is is fine.

0

Activated is good, I agree.

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