Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

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

Asked by
raid6n 2196 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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

Here's the script:

01LocalPlayer = game.Players.LocalPlayer
02tableLots = script.Parent.Parent.Parent.TableLots
03 
04script.Parent.MouseButton1Click: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.BackgroundColor3 = Color3.fromRGB(255,0,0)
13elseif
14chosenTable.BackgroundColor3 == Color3.fromRGB(255,0,0) then
15    script.Parent.Text = "Table already taken."
View all 21 lines...

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

It would be wonderful if someone explained the elseif part.

0
Else if is written as "elseif" not "else if" MakeYourEscape 334 — 5y
2
I tried that, didn't work, just a typo in the code block. raid6n 2196 — 5y
0
nigger fangsrevive 0 — 2y
0
fuck you NIGGER raid6n 2196 — 2y

2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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:

01LocalPlayer = game.Players.LocalPlayer
02tableLots = script.Parent.Parent.Parent.TableLots
03 
04script.Parent.MouseButton1Click: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.BackgroundColor3 == Color3.fromRGB(255,0,0))then
13           script.Parent.Text = "Table already taken."
14           wait(3)
15           script.Parent.Text = "Please try again."
View all 24 lines...
Ad
Log in to vote
1
Answered by 5 years ago

There are a few things wrong.

01-- these variables should be local because i'm assuming this isn't inside a function
02local LocalPlayer = game.Players.LocalPlayer
03local tableLots = script.Parent.Parent.Parent.TableLots
04 
05-- changed "connect" to "Connect", since "connect" is deprecated.
06script.Parent.MouseButton1Click: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.BackgroundColor3 = Color3.fromRGB(255,0,0)
15-- elseif should be put together.
View all 24 lines...

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.

0
Only the Text part didn't work, everything else is fine. raid6n 2196 — 5y
0
Stop begging for accepts. Trollapse 89 — 5y
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. royaltoe 5144 — 5y
0
Activated is good, I agree. royaltoe 5144 — 5y

Answer this question