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

Ifs and Loops?

Asked by 8 years ago

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?

0
So, what exactly do you need help with? Your question is kind of confusing. Monsieur_Robert 338 — 8y
0
Don't use elseif for that! USE ELSE! BlueTaslem 18071 — 8y

1 answer

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

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.
Ad

Answer this question