First, I typed in a local script into a text button.
Here's the script:
01 | LocalPlayer = game.Players.LocalPlayer |
02 | tableLots = script.Parent.Parent.Parent.TableLots |
03 |
04 | script.Parent.MouseButton 1 Click:connect( function () |
05 | local text = script.Parent.Parent.TableHandler.Text |
06 | local number = tonumber (text) |
07 | print ( "Number " , number) |
08 |
09 | if number > = 1 and number < = 18 then |
10 | local chosenTable = tableLots [ "Table" .. tostring (number) ] |
11 | print ( "Table " , chosenTable) |
12 | chosenTable.BackgroundColor 3 = Color 3. fromRGB( 255 , 0 , 0 ) |
13 | elseif |
14 | chosenTable.BackgroundColor 3 = = Color 3. fromRGB( 255 , 0 , 0 ) then |
15 | script.Parent.Text = "Table already taken." |
As I type in elseif and the continued script, nothing works.
It would be wonderful if someone explained the elseif part.
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.
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.
01 | LocalPlayer = game.Players.LocalPlayer |
02 | tableLots = script.Parent.Parent.Parent.TableLots |
03 |
04 | script.Parent.MouseButton 1 Click:Connect( function () |
05 | local text = script.Parent.Parent.TableHandler.Text |
06 | local number = tonumber (text) |
07 | print ( "Number " , number) |
08 |
09 | if number > = 1 and number < = 18 then |
10 | local chosenTable = tableLots [ "Table" .. tostring (number) ] |
11 |
12 | if (chosenTable.BackgroundColor 3 = = Color 3. fromRGB( 255 , 0 , 0 )) then |
13 | script.Parent.Text = "Table already taken." |
14 | wait( 3 ) |
15 | script.Parent.Text = "Please try again." |
There are a few things wrong.
01 | -- these variables should be local because i'm assuming this isn't inside a function |
02 | local LocalPlayer = game.Players.LocalPlayer |
03 | local tableLots = script.Parent.Parent.Parent.TableLots |
04 |
05 | -- changed "connect" to "Connect", since "connect" is deprecated. |
06 | script.Parent.MouseButton 1 Click:Connect( function () |
07 | local text = script.Parent.Parent.TableHandler.Text |
08 | local number = tonumber (text) |
09 | print ( "Number " , number) |
10 |
11 | if number > = 1 and number < = 18 then |
12 | local chosenTable = tableLots [ "Table" .. tostring (number) ] |
13 | print ( "Table " , chosenTable) |
14 | chosenTable.BackgroundColor 3 = Color 3. fromRGB( 255 , 0 , 0 ) |
15 | -- elseif should be put together. |
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.