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

How to make background gui change color based off string value?

Asked by 3 years ago

This is what I have so far! it only works if the string value in the workspace turns to it right away would I need to use .Changed some how?

while true do
    if script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.announcement.Value == "No Announcements" then
        script.Parent.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
        wait()
    end

    if script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.announcement.Value == "Tornado Watch" then
        script.Parent.BackgroundColor3 = Color3.fromRGB(255, 170, 0)
        wait()
    end

    if script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Workspace.announcement.Value == "Tornado Warning" then
        script.Parent.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
        wait()
    end
end
0
for some reason the .Value didn't show up in this codeblock Chasers0919 -1 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Try to avoid using any loops that only detects simple changes/updates

local announcement = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.announcement -- Define announcement, easier to type instead of .Parent 7 times everytime.
announcement:GetPropertyChangedSignal("Value"):Connect(function() -- Detect when announcement's value is changed, and when it is run below code
    if announcement.Value == "No Announcement" then -- Start off with if.
        script.Parent.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    elseif announcement.Value == "Tornado Watch" then -- Continue all others with elseif.
        script.Parent.BackgroundColor3 = Color3.fromRGB(255, 170, 0)
    elseif announcement.Value == "Tornado Warning" then
        script.Parent.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
    end -- Only one end to wrap up all of the if statements. The code will start at the top and if true it will change it to black. If its false it'll move on and continue until true. Faster than running multiple if statements that each have end's
end)

-- Overall running a while true do to update a simple variable that only changes every so often is resources wasted. You should only run the script when the value itself is changed, therfor only running when it needs to.
0
Should it be in a local script or a normal script? Chasers0919 -1 — 3y
0
Local script if its a player gui. If you are using a surface/billboard gui to display it all, a script would work. WizyTheNinja 834 — 3y
0
Ok thank you so much Chasers0919 -1 — 3y
0
No problem, goodluck! WizyTheNinja 834 — 3y
Ad

Answer this question