Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Error with else?

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)

3 comments

0

any error in output?

0

No.

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.

3 answers

✓ Accepted answer

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

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!

3 comments

0

Dyler summed it up perfectly, this just highlights the problem with using the if statement in two blocks of code

0

Ah, I should've explained it a bit more I see. Anyways, thanks for doing what I forgot. Upvoted

0

Nah i think both answers were good, I just didn't know how to provide a solution

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.