Jump to content




How do I get a boolean to become false? I've tried everything and it doesn't work


5 replies to this topic

#1 NewBuildmini

  • Members
  • 4 posts

Posted 05 November 2020 - 01:14 AM

I SOLVED THIS EVENTUALLY

So I have a script for my traffic control modded traffic lights. I have 3 redstone wires the computer can send a signal to. That part works. There's another redstone wire on the front, to which a night sensor is connected. It should make the lights flash yellow, which it does. But my problem is I can't get the flash to turn off, and for it to resume the cycle once the night sensor's redstone signal stops. I've tried local nightmode = false, and and night = not night, but it still won't do the job I want it to do.
The sides are: Right is red, Back is green, Left is flashing yellow (there is no steady yellow phase because it automatically turns on when both red and green are false)
Here's the script.
local night = false
local nightmode = false
while not night do
rs.setOutput("right", true)
rs.setOutput("back", false)
sleep(15)
rs.setOutput("right", false)
rs.setOutput("back", false)
sleep(3)
rs.setOutput("right", false)
rs.setOutput("back", true)
sleep(15)
rs.setOutput("right", false)
rs.setOutput("back", false)
rs.setOutput("left", false)
sleep(3)
if rs.getInput("front") then
break
end
local night = true
local nightmode = true
end
while not nightmode do
rs.setOutput("right", false)
rs.setOutput("back", false)
sleep(1)
rs.setOutput('left", true)
--Problem below!
if rs.getInput("front") == false then
break
end
local nightmode = not nightmode
local night = not night
end

Edited by NewBuildmini, 26 November 2020 - 09:57 AM.


#2 Lupus590

  • Members
  • 2,027 posts
  • LocationUK

Posted 15 November 2020 - 08:50 PM

it sounds like you have something else going wrong as what you have said that you have tried is how you are meant to do it.

Given that you are using redstone, check that you don't have any other redstone sources that could be causing interference.

#3 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 20 November 2020 - 08:00 PM

Lupus you're getting rusty...

local night = false
local nightmode = false
while not night do
	rs.setOutput("right", true)
	rs.setOutput("back", false)
	sleep(15)
	rs.setOutput("right", false)
	rs.setOutput("back", false)
	sleep(3)
	rs.setOutput("right", false)
	rs.setOutput("back", true)
	sleep(15)
	rs.setOutput("right", false)
	rs.setOutput("back", false)
	rs.setOutput("left", false)
	sleep(3)
	if rs.getInput("front") then
		break
	end
	local night = true -- ISSUE
	local nightmode = true --ISSUE
end
while not nightmode do
	rs.setOutput("right", false)
	rs.setOutput("back", false)
	sleep(1)
	rs.setOutput("left", true)
	if rs.getInput("front") == false then
		break
	end
	local nightmode = not nightmode --ISSUE
	local night = not night --ISSUE
end

All of the lines marked as ISSUE are creating new variables inside the loops. They are not modifying the earlier variables as you used the local keyword. Remove the local keyword and you're in business.

Remember, use local only when you're creating the variable to start with. If you use it afterwards, the code just makes a new variable and hides the earlier one.

Edited by Dragon53535, 20 November 2020 - 08:00 PM.


#4 NewBuildmini

  • Members
  • 4 posts

Posted 21 November 2020 - 01:39 AM

I've tried this already, but removing local just breaks the script.

#5 NewBuildmini

  • Members
  • 4 posts

Posted 26 November 2020 - 09:56 AM

Nvm, solved this. I used reboot instead of doing break in "not nightmode" script.

#6 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 26 November 2020 - 10:54 PM

That's mostly because of the way your code is set up. You actually don't have any while loops.
while not night do
    --Blah blah
    night = true
end
Changing night to true breaks out of the loop anyways. So you in essence don't have any loops at all.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users