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

Why wont this light switch work? I dont think theres anotheer way

Its suppose to be a light switch system but doesnt work after the first if.

The game is FE (Dont think it matters since its a server script)

local Switch1 = script.Parent.Switch1 --Transperent if on--

local Switch2 = script.Parent.Switch2--Transperent if off--

local SwitchSound = script.Parent.Frame.Switch

local PopSound = script.Parent.Bulb.Pop

local Light = script.Parent.Bulb.Light



script.Parent.Frame.FlickSwitch.MouseClick:Connect(function()
	if script.Parent.IsOn.Value == false then --If Not on then--
		if script.Parent.IsBroken.Value == false then --Check if its broken--
			SwitchSound:Play()
			Switch1.Transparency = 1
			Switch2.Transparency = 0
			Light.Enabled = true
			script.Parent.IsOn.Value = true
			print("Is On")
		else
			if script.Parent.IsBroken.Value == true then
				SwitchSound:Play()
				Switch1.Transparency = 1
				Switch2.Transparency = 0
				script.Parent.IsOn.Value = true
				print("Is On but is broken")
			else
				if script.Parent.IsBroken.Value == false then
					--Turn off the light normally--
					SwitchSound:Play()
					Switch1.Transparency = 0
					Switch2.Transparency = 1
					Light.Enabled = false
					script.Parent.IsOn.Value = false
					print("Is Off")
				else
					SwitchSound:Play()
					Switch1.Transparency = 1
					Switch2.Transparency = 0
					script.Parent.IsOn.Value = true
					print("Is On but broken")
				end
			end
		end
	end
end)

while wait(1) do
	if script.Parent.IsOn.Value == true then
		script.Parent.Hours.Value = script.Parent.Hours.Value - 2
	else
		
	end
end


while wait(1) do
	if script.Parent.Hours.Value <= 0 and script.Parent.IsBroken.Value == false then
		Light.Enabled = false
		PopSound:Play()
		script.Parent.IsBroken.Value = true
	end
end

2 comments

0

Does this give an error?

0

No, all of his code is correct. Look at lines 50 through 65. There are two while loops. The second while loop wouldn't be reached because the first while loop is going on infinetly

2 answers

✓ Accepted answer

##There are two solutions to your problem

Your script does not continue on because the while loop on lines 50 through 56 goes on infinitely. If you make a while loop,(without breaking it) it will go on assuming you have made your while loop conditional to a delay(while wait() do) or true(while true do). You could use coroutines like this:

... -- These eclipses are just for you to know that there is code above the coroutines
-- There was an extra "else" statement that was unneeded in this loop
-- I am also assuming you have the variables declared above
local turnOn = coroutine.wrap(function()
	while wait(1) do
    		if script.Parent.IsOn.Value then
        		script.Parent.Hours.Value = script.Parent.Hours.Value - 2
    		end
	end
end)

local isBroken = coroutine.wrap(function()
	while wait(1) do
	    	if script.Parent.Hours.Value <= 0 and not script.Parent.IsBroken.Value then
	        	Light.Enabled = false
	        	PopSound:Play()
	        	script.Parent.IsBroken.Value = true
		end
	end
end)

turnOn()
isBroken()

I highly suggest taking a look at Roblox Wiki's documentation on coroutines here.

Now, this is a valid way to fix the script. However, if you were to add a lot of lights, your game would experience a lot of lag because of two while loops using if statements after a second. That will be multiplied with the numerous amount of lights you have. A way to optimize this is by adding the code inside of the second loop into the first loop. You could optimize this even more by adding a Changed event. Optimizing is just making code to be more efficient and faster.

Let's take a look at this event:

...
script.Parent.IsOn.Changed:Connect(function()
	script.Parent.Hours.Value = script.Parent.Hours.Value - 2
	if script.Parent.Hours.Value <= 0 and not script.Parent.IsBroken.Value then
		Light.Enabled = false
	        PopSound:Play()
	        script.Parent.IsBroken.Value = true
	end
end)

The difference between the Changed event and while loops is that while loops always run(in your case). However, the Changed event runs when a property has been changed. Since you are working with IntValues and BoolValues, we can assume that the only property that will be changed is the value(in your script of course).

Then, when the hour's value has been changed, we check if it is below or equal to 0 and if IsBroken is false (not script.Parent.IsBroken.Value is the same as script.Parent.IsBroken.Value == false and script.Parent.IsBroken.Value is the same thing as script.Parent.IsBroken.Value == true)

Next, we do your if statement that you have done in another loop and put it after subtracting Hour's value.

NOTE: IF THIS WAS NOT THE SOLUTION YOU WERE LOOKING FOR, PLEASE TELL ME. IF THE OPTIMIZED WAYS WERE NOT WHAT YOU WANTED, USE COROUTINES

Here is the final code:

local Switch1 = script.Parent.Switch1 --Transperent if on--

local Switch2 = script.Parent.Switch2--Transperent if off--

local SwitchSound = script.Parent.Frame.Switch

local PopSound = script.Parent.Bulb.Pop

local Light = script.Parent.Bulb.Light script.Parent.Frame.FlickSwitch.MouseClick:Connect(function()
    if not script.Parent.IsOn.Value then --If Not on then--
        if script.Parent.IsBroken.Value == false then --Check if its broken--
                SwitchSound:Play()
                Switch1.Transparency = 1
                Switch2.Transparency = 0
                Light.Enabled = true
                script.Parent.IsOn.Value = true
                print("Is On")
        elseif script.Parent.IsBroken.Value == true then
                SwitchSound:Play()
                Switch1.Transparency = 1
                Switch2.Transparency = 0
                script.Parent.IsOn.Value = true
                print("Is On but is broken")
        elseif script.Parent.IsBroken.Value == false then
                --Turn off the light normally--
                 SwitchSound:Play()
                 Switch1.Transparency = 0
                 Switch2.Transparency = 1
                 Light.Enabled = false
                 script.Parent.IsOn.Value = false
                 print("Is Off")
         else
                SwitchSound:Play()
                Switch1.Transparency = 1
                Switch2.Transparency = 0
                script.Parent.IsOn.Value = true
                print("Is On but broken")
          end
      end
end)

script.Parent.IsOn.Changed:Connect(function()
	script.Parent.Hours.Value = script.Parent.Hours.Value - 2
	if script.Parent.Hours.Value <= 0 and not script.Parent.IsBroken.Value then
		Light.Enabled = false
	        PopSound:Play()
	        script.Parent.IsBroken.Value = true
	end
end)

I have left a few side notes.

You should use :WaitForChild(). This is extremely useful when you are not sure when an object has loaded or not.

You should store objects that you use constantly in variables. This can be extremely useful because you won't have to type the same thing again. You will simply have to type a word(or two) to simply access that object. This can also be useful when you are trying to debug or fix a script.

2 comments

0

Same thing but I think I know the problem noww. Time to fix it lmao

0

Your welcome lel

try this

local Switch1 = script.Parent:WaitForChild("Switch1")
local Switch2 = script.Parent:WaitForChild("Switch2")
local SwitchSound = script.Parent:WaitForChild("Frame"):WaitForChild("Switch")
local PopSound = script.Parent:WaitForChild("Bulb"):WaitForChild("Pop")
local Light = script.Parent.Bulb:WaitForChild("Light")

1 comment

0

DIdnt work, I dont think thats the problem

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