Can't figure out how to script a button

Trying to make a flashing Safety Car sign for a racing game, got the sign to turn on and work as I wanted to but I cannot seem to figure out how to get it to turn off.

My script:


function  onClicked()  
	
	lightOn = true
	
	while lightOn == true do
		
		repeat
			workspace.sclight.s.BrickColor = BrickColor.new("Institutional white")
			workspace.sclight.s.Material = ("Neon") 

			workspace.sclight.c.BrickColor = BrickColor.new("Institutional white")
			workspace.sclight.c.Material = ("Neon") 

			workspace.sclight.amber.BrickColor = BrickColor.new("Deep orange")
			workspace.sclight.amber.Material = ("Neon")

			wait(0.25)

			workspace.sclight.amber.BrickColor = BrickColor.new("Reddish brown")
			workspace.sclight.amber.Material = ("Plastic")

			wait(0.25)

			
		until lightOn == false
		
	end	
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

sclight.s being the “S”
sclight.c being the “C”
sclight.amber being the flashing square around the text

1 Like

you want it to repeteadly turn on and off like american signs?

Yep, that’s what ideally it should look like

Why not adding your loop code inside another script, parenting this script to the sign and clicking the button would just disabled or enabled the script inside the button?

What would that script look like, I’m relatively new to scripting and I’m not sure what I should be doing

i would do something like:

script.Parent.ClickDetector.MouseClick:connect(function()
if YourSignScript.Disabled == false then
YourSignScript.Disabled = true
else
YourSignScript.Disabled = false
end
end)

Alright thanks, let me put it into the code

it is quite a simple way but sometimes, the most simple code is better for something that simple :stuck_out_tongue:

1 Like
if workspace.sclight.lightscript.Disabled == true then
	repeat
		workspace.sclight.s.BrickColor = BrickColor.new("Institutional white")
		workspace.sclight.s.Material = ("Neon") 

		workspace.sclight.c.BrickColor = BrickColor.new("Institutional white")
		workspace.sclight.c.Material = ("Neon") 

		workspace.sclight.amber.BrickColor = BrickColor.new("Deep orange")
		workspace.sclight.amber.Material = ("Neon")

		wait(0.25)

		workspace.sclight.amber.BrickColor = BrickColor.new("Reddish brown")
		workspace.sclight.amber.Material = ("Plastic")

		wait(0.25)


	until workspace.sclight.lightscript.Disabled == false
	
else 	
	workspace.sclight.s.BrickColor = BrickColor.new("Dark stone grey")
	workspace.sclight.s.Material = ("Plastic") 

	workspace.sclight.c.BrickColor = BrickColor.new("Dark stone grey")
	workspace.sclight.c.Material = ("Plastic") 

	workspace.sclight.amber.BrickColor = BrickColor.new("Reddish brown")
	workspace.sclight.amber.Material = ("Plastic")

end

end 

Is this what the script inside the sign should look like?

more something like this:

while true do
workspace.sclight.s.BrickColor = BrickColor.new("Institutional white")
workspace.sclight.s.Material = ("Neon") 

workspace.sclight.c.BrickColor = BrickColor.new("Institutional white")
workspace.sclight.c.Material = ("Neon") 

workspace.sclight.amber.BrickColor = BrickColor.new("Deep orange")
workspace.sclight.amber.Material = ("Neon")

wait(0.25)

workspace.sclight.amber.BrickColor = BrickColor.new("Reddish brown")
workspace.sclight.amber.Material = ("Plastic")

wait(0.25)
end

[/quote]

also, here is a simplified code with variables:

local sclight = workspace.sclight.s
local cclight = workspace.sclight.c
local aclight = workspace.sclight.amber

while true do
sclight.BrickColor = BrickColor.new("Institutional white")
sclight.Material = ("Neon") 

cclight.BrickColor = BrickColor.new("Institutional white")
cclight.Material = ("Neon") 

aclight.BrickColor = BrickColor.new("Deep orange")
aclight.Material = ("Neon")

wait(0.25)

aclight.BrickColor = BrickColor.new("Reddish brown")
aclight.Material = ("Plastic")

wait(0.25)
end

Okay, put the code in. Whenever I test in studio, the lights are already on and flashing, however when the button is pressed it essentially pauses the script, should I add something like this to the script?

while false do
sclight.BrickColor = BrickColor.new("Dark stone grey")
sclight.Material = ("Plastic") 

cclight.BrickColor = BrickColor.new("Dark stone grey")
cclight.Material = ("Plastic") 

aclight.BrickColor = BrickColor.new("Reddish Brown")
aclight.Material = ("Plastic")

end
1 Like

it isn’t necessary because the script button i gave you before litteraly disabled the script or enabled it. When you enable a disabled script, it does restart him. You want to fully shut down the light when disabled? just set the material and brickcolor when you disable the script, on the button script

Ah okay, will do just that then, thanks!

Hope it fixed your problem :+1:

Hmm, you know you can simply invert a variable right? 5 lines of code…

function  onClicked()  
	lightOn = not lightOn -- This is how you invert a boolean
	while lightOn do
		workspace.sclight.s.BrickColor = BrickColor.new("Institutional white")
		workspace.sclight.s.Material = ("Neon") 

		workspace.sclight.c.BrickColor = BrickColor.new("Institutional white")
		workspace.sclight.c.Material = ("Neon") 

		workspace.sclight.amber.BrickColor = BrickColor.new("Deep orange")
		workspace.sclight.amber.Material = ("Neon")

		wait(0.25)

		workspace.sclight.amber.BrickColor = BrickColor.new("Reddish brown")
		workspace.sclight.amber.Material = ("Plastic")

		wait(0.25)		
	end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

I there may be a better way to write this code but I think that solution you gave already should do the trick.

1 Like

Yeah my solution is a bit hacky, but for a simple thing like that, it should do the job i guess

1 Like

This script is working the best so far, problem is now when I click the button again, the letters are still in their on state, what should I do?

the letters are lighted on when you want to light em off?

Just before the while true loop do a if statement that puts the switch in their off state if lightOn == false

1 Like