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

Error with else?

Asked by 8 years ago

Problem: i have this code to update a gui baste on game values, and when i added the second changed function, the else was underlined in red indicating an error, and i was hoping someone could help me out :D THANKS.

local player = game.Players.LocalPlayer
game.Workspace.Intermission.Changed:connect(function()
if game.Workspace.Intermission.Value == true then
    script.Parent.Parent.Visible = true
game.Workspace.Inttime.Changed:connect(function()
local Inttime = workspace.Inttime.Value
script.Parent.Plrname.Text = player.Name
script.Parent.Inttime.Text = (Inttime.. " Until Round")
script.Parent.BackgroundColor = player.TeamColor
else
script.Parent.BackgroundColor = BrickColor.White()
end
end)
end)
0
any error in output? iNicklas 215 — 8y
0
No. viralmoose 65 — 8y
0
You can't write the else after you have wrote "game.Workspace.Inttime.Changed:connect(function()" it has to be before your if statement end. Scriptree 125 — 8y

3 answers

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
8 years ago

You just used it in the wrong place in your code. You need to continue an "else" statement off of an existing "if" statement, in the same block of code.

(Also, you should try to use indentations, it helps keep your code cleaner, and makes it way easier for us to help you debug it)

So try this:

local player = game.Players.LocalPlayer
    game.Workspace.Intermission.Changed:connect(function()
    if game.Workspace.Intermission.Value == true then
        script.Parent.Parent.Visible = true
        game.Workspace.Inttime.Changed:connect(function()
            local Inttime = workspace.Inttime.Value
            script.Parent.Plrname.Text = player.Name
            script.Parent.Inttime.Text = (Inttime.. " Until Round")
            script.Parent.BackgroundColor = player.TeamColor
        end)
    else
        script.Parent.BackgroundColor = BrickColor.White()
    end
end)

Anyways, it should work now. If you have any further questions/problems please leave a comment below. Hope I helped :P

Ad
Log in to vote
1
Answered by 8 years ago

You are calling a totally new function before ending the other function. You need to move the else out of the other function or figure out how to avoid having the else in another function.

local player = game.Players.LocalPlayer
game.Workspace.Intermission.Changed:connect(function() --FUNCTION 1 | This is ok!
if game.Workspace.Intermission.Value == true then -- If statement
    script.Parent.Parent.Visible = true
game.Workspace.Inttime.Changed:connect(function() -- FUNCTION 2 | ERROR you did not close the if statement, this will not work!!!
local Inttime = workspace.Inttime.Value
script.Parent.Plrname.Text = player.Name
script.Parent.Inttime.Text = (Inttime.. " Until Round")
script.Parent.BackgroundColor = player.TeamColor
else --This cannot be in another function
script.Parent.BackgroundColor = BrickColor.White()
end
end)
end)

Please put some spacing in your script above so that if you need help figuring how to get the if statement in one function or accomplish this script with a different method, I can help. I hope this helps you see your issue. Let me know if this is wrong!

0
Dyler summed it up perfectly, this just highlights the problem with using the if statement in two blocks of code dragonkeeper467 453 — 8y
0
Ah, I should've explained it a bit more I see. Anyways, thanks for doing what I forgot. Upvoted dyler3 1510 — 8y
0
Nah i think both answers were good, I just didn't know how to provide a solution dragonkeeper467 453 — 8y
Log in to vote
0
Answered by 8 years ago

It looks like it underlined else because it was expecting an end for the function before the else. Try this:

local player = game.Players.LocalPlayer
game.Workspace.Intermission.Changed:connect(function()
if game.Workspace.Intermission.Value == true then
    script.Parent.Parent.Visible = true
    game.Workspace.Inttime.Changed:connect(function()
        local Inttime = workspace.Inttime.Value
        script.Parent.Plrname.Text = player.Name
        script.Parent.Inttime.Text = (Inttime.. " Until Round")
        script.Parent.BackgroundColor = player.TeamColor
        end)
    else
        script.Parent.BackgroundColor = BrickColor.White()
    end
end)

Answer this question