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

Ifs and Loops?

I'm trying to make a SurfaceGUI with a GUI, which are linked, you press a button in the gui which changes a value, That works.

What I'm trying to do now is have the surface gui display one of two phases. Custom Messages, or looped messages.

local nextMsg = 20

local titles = { --Titles
    "Need Help?";
    "REMEMBER:";
    "REMEMBER:";
}
local title = script.Parent.Title
local cont = script.Parent.Content
local content = { --The content
    "Use the 911 callbox to get help!";
    "Stick to the speed limit! People are trying not to get hurt!";
    "Trespassing is a crime! Trespassing on other's properties will get you arrested!";
}
math.randomseed(os.time())
local emergency = workspace.CMessages.Value
while wait()  do 
	if emergency == false then
	   	for i, r in ipairs(titles) do
	        title.Text = titles[i];
	        cont.Text = content[i];
	        wait(nextMsg)
	    end
	elseif emergency == true then
		title.Text = "Park Emergency"
		cont.Text = game.Workspace.CMessages.EMessage.Value
	end
end

The question for those who don't understand

How do I get this to work, because when I change the value to true, it changes, but when I change it to false, it doesn't go back to the looping. Why?

2 comments

0

So, what exactly do you need help with? Your question is kind of confusing.

0

Don't use elseif for that! USE ELSE!

1 answer

✓ Accepted answer

##Problem

Your emergency variable was being set outside of the while loop. While the while loop was running, the value could never be changed.


##Solution

I have modified the script to use the .Changed event since it is a more efficient way of checking if a value has changed rather than a while loop. Every time the Changed event is fired the script will provide the bool value. The script will go through the if statement and will make the necessary changes. I removed the math.randomseed() function since you never used math.random() in your script.


##Final Script

local nextMsg = 20

local titles = { --Titles
    "Need Help?";
    "REMEMBER:";
    "REMEMBER:";
}
local title = script.Parent.Title
local cont = script.Parent.Content
local content = { --The content
    "Use the 911 callbox to get help!";
    "Stick to the speed limit! People are trying not to get hurt!";
    "Trespassing is a crime! Trespassing on other's properties will get you arrested!";
}

workspace.CMessages.Changed:connect(function(emergency)
    if emergency then
        title.Text = "Park Emergency"
        cont.Text = game.Workspace.CMessages.EMessage.Value
    else
        for i,r in ipairs(titles) do
            title.Text = titles[i]
            cont.Text = content[i]
            wait(nextMsg)
        end
    end
end)

######Hopefully this answered your question, if so do not forget to hit the accept answer button. If you have any questions, feel free to comment below.

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