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

How can I fix my elseif statement?

Asked by
Hypgnosis 186
5 years ago

Hello. I am trying to write a piece of code that allows a player to edit their overhead gui. This particular function concerns the player's ability to toggle on/off the name.

On line 6, there may be an error if the user has not yet entered a name. If they have not, an error shows if they try to toggle their name back on. "(string expected, got nil)"

Obviously, I tried to rectify that on the next elseif statement, but the program stops reading at the error.

Is there any way to get past this?

remote.OnServerEvent:Connect(function(Player)
    if comClone.Data.Text == nameEntered or comClone.Data.Text == Player.Name then
        comClone.Data.Text = ""

    elseif comClone.Data.Text == "" then
        comClone.Data.Text = nameEntered

    elseif nameEntered == nil then
        comClone.Data.Text = Player.Name
    end
end)
1
You're using nameEntered in the second clause, even though you know it can be nil (since you're checking for this in the 3rd clause). Why not reverse the order of those checks? EmilyBendsSpace 1025 — 5y
1
TextLabels also have a Visible property you can use to hide them, though this might not be an option if you are using something like a UIListLayout, where the layout would change. EmilyBendsSpace 1025 — 5y
0
Thanks, I will check both of these out. Hypgnosis 186 — 5y

Answer this question