So I just started learning code, and I thought it would be fun to make a racing traffic light. So I did, the traffic light works perfectly fine, but the textbutton (in surfaceGUI) doesn't change and sometimes doesn't show up. How do I fix this/what did I do wrong?
local textbuttonText = game.Workspace.SurfaceGui.TextButton.Text while true do game.Workspace.Part2.BrickColor = BrickColor.new("Really black") game.Workspace.Part1.BrickColor = BrickColor.new("Really red") textbuttonText = "Ready?" wait(3) game.Workspace.Part1.BrickColor = BrickColor.new("Really black") game.Workspace.Part3.BrickColor = BrickColor.new("Neon orange") textbuttonText = "Set?" wait(3) game.Workspace.Part3.BrickColor = BrickColor.new("Really black") game.Workspace.Part2.BrickColor = BrickColor.new("Bright green") textbuttonText = "Go!" wait(3) end
Howdy! You see, you indexed the value TextbuttonText to game.Workspace.SurfaceGui.TextButton.Text. That means, changing textbuttonText won't affect game.Workspace.SurfaceGui.TextButton.Text at all.
local textbuttontext = game.Workspace.SurfaceGui.TextButton.Text textbuttontext = "e" --will change the variable textbuttontext game.Workspace.SurfaceGui.TextButton.Text = "e" --will change textbutton's property, Text
Meaning, game.Workspace.SurfaceGui.TextButton.Text =/= textbuttonText.
A fix to this is to just:
local textbutton = game.Workspace.SurfaceGui.TextButton while true do game.Workspace.Part2.BrickColor = BrickColor.new("Really black") game.Workspace.Part1.BrickColor = BrickColor.new("Really red") textbutton.Text = "Ready?" --textbutton dot Text wait(3) game.Workspace.Part1.BrickColor = BrickColor.new("Really black") game.Workspace.Part3.BrickColor = BrickColor.new("Neon orange") textbutton.Text= "Set?" wait(3) game.Workspace.Part3.BrickColor = BrickColor.new("Really black") game.Workspace.Part2.BrickColor = BrickColor.new("Bright green") textbutton.Text= "Go!" wait(3) end
If you have any questions, ask them below in this answer's comment section.
local textbutton = workspace.SurfaceGui.TextButton -- using "workspace" instead of "game.Workspace" is much faster on typing time and slightly faster on indexing time local p1 = workspace.Part1 local p2 = workspace.Part2 local p3 = workspace.Part3 -- index your parts before continuing with the script to save time, in some rare cases you will not need to do this (usually anti exploit or remaking an instance) local waittime = 3 -- if you are using the same wait time for all three of the waits, create a variable so it is easier to edit while true do textbutton.Visible = true -- if it is not showing up you may have an issue in another script or a different part of the code you are not showing me, fix it or use this to guarantee it shows up p2.BrickColor = BrickColor.new("Really black") p1.BrickColor = BrickColor.new("Really red") textbutton.Text = "Ready?" wait(waittime) p1.BrickColor = BrickColor.new("Really black") p3.BrickColor = BrickColor.new("Neon orange") ttextbutton.Text = "Set?" wait(waittime) p3.BrickColor = BrickColor.new("Really black") p2.BrickColor = BrickColor.new("Bright green") textbutton.Text = "Go!" wait(waittime) end