Basically, I have a table of bools all set to false. At some point, one or more of the bools will be set to true.
Here’s my script
looking = false
while true do
local players = script.Parent.lookatme.players:GetChildren()
for i, player in pairs(players) do
if player.Value == true then
looking = true
end
end
wait(.1)
end
The only problem I have with this script is that I can’t find a way to turn “looking” to false.
Set looking = false after the while true do line and just use local looking where you have it now.
Although, it seems like there may be a better way to do this rather than polling the looking variable all the time (which I assume you are doing somewhere else).
I may not be understanding the problem entirely, but it looks like once looking is set to true it never becomes false again. To fix that you can do this:
local looking --looking = false (although prob not needed if you've declared it earlier in the code)
while true do
looking = false -- reset to false here each time you check
local players = script.Parent.lookatme.players:GetChildren()
for i, player in pairs(players) do
if player.Value == true then
looking = true
end
end
wait(.1)
end
So that will continuously check your table with the looking value possibly changing every .1 sec.
Ah, I see. I should of explained the problem a bit more.
The thing is, as long as at least one value is true, I want “looking” to stay true, until ALL values are set back to false. I don’t want “looking” to cycle from true to false, true to false repeatedly.
You won’t see it switching because it will turn false then true again before the wait() releases the loop for other code to check the value. If all the bools are false then it will stay false, otherwise it will change to true. Either way, other code will only see looking=false if all the bools in your table are false and looking=true if at least one is true.
Another way to do that would be to change the looking variable only when something changes in the table. If you set a bool to true then you also set looking to true. If you set a bool to false then you’d have to scan the whole table to see if all the others are false (though you can break early if you find a bool that’s true).
Could also do this with a sort of reference counter variable. Every time a bool is set to true you add 1 to the ref and every time a bool is set to false you decrement the variable. Instead of using a boolean looking variable, you check that ref counter and consider looking to be true if the ref is > 0 and false if it’s 0.
There are probably a bunch of other ways to handle it, but I’m drawing a blank… It’s bedtime XD
You will just need to set looking to false at the start of the while loop:
while true do
looking = false
local players = script.Parent.lookatme.players:GetChildren()
for i, player in pairs(players) do
if player.Value == true then
looking = true
end
end
wait(.1)
end
Also if you are just checking once, you should use a function.
local function CheckLooking()
looking = false
local players = script.Parent.lookatme.players:GetChildren()
for i, player in pairs(players) do
if player.Value == true then
looking = true
end
end
end
return looking
end
print(CheckLooking())