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

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

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

1 comment

0

for some reason the .Value didn't show up in this codeblock

1 answer

✓ Accepted answer

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.

4 comments

0

Should it be in a local script or a normal script?

0

Local script if its a player gui. If you are using a surface/billboard gui to display it all, a script would work.

0

Ok thank you so much

0

No problem, goodluck!

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