Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
4

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

Asked by
raid6n 2309 Moderation Voter Community Moderator
1 year ago
Edited 1 year ago

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.

0
Else if is written as "elseif" not "else if" MakeYourEscape 334 — 1y
2
I tried that, didn't work, just a typo in the code block. raid6n 2309 — 1y

2 answers

Log in to vote
1
Answered by
n7e 5118 Moderation Voter Community Moderator
1 year ago
Edited 1 year 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:

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)
Ad
Log in to vote
1
Answered by 1 year ago

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.

0
Only the Text part didn't work, everything else is fine. raid6n 2309 — 1y
0
Stop begging for accepts. blocktaco 89 — 1y
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. n7e 5118 — 1y
0
Activated is good, I agree. n7e 5118 — 1y

Answer this question