I have a table, where either the bools inside of it are set to true or false. When it finds a bool that is true, it runs a function. When it finds a bool that is false, it completely stops that function. However, using “in pairs” to scan the table causes a problem. If a bool is true, but the bools after that are false, the function runs but is immediately stopped.
Basically, what I’m trying to achieve is, if ATLEAST ONE bool is true, the function runs, regardless of how many there are.
Loop through the table, and check if the current Value you’re looping through is true, if so, just call the function. Example:
local coolTable = {Cheese = false, Burger = false, Cola = true}
local function do_Something()
warn("This value is true!")
end
for _, v in ipairs(coolTable) do
if v == true then
do_Something()
else
warn("This value is false!")
end
end
Ah, shoot. I actually forgot to mention:
The bool value checker is on a loop, meaning it will always be checking if any of the bools are true or not. This is one of the things that cause the problem I’m having.
local coolTable = {Cheese = false, Burger = false, Cola = true}
while wait(2) do
for _, v in ipairs(coolTable) do
if v == true then
-- Anything
else
break
end
end
end
Here’s the script. In the original thread I had simplified what I had done by a lot so that it was easy to understand and apply to any situation, I’ll just explain what is happening in the script
There are multiple stringvalues in the workspace. If any of the stringvalues == the players name, then a gui will trigger inside that player. If none of the stringvalues == the players name, then it will do the opposite and disable the gui (it only disables the gui if the gui is already enabled)
local plrname = game.Players.LocalPlayer.Character.Name
local ifchased = game.Workspace.monsterchasewho:GetChildren()
local toggle = false
while true do
for i, monster in pairs(ifchased) do
if monster.Value == plrname then
if toggle == false then
toggle = true
script.Parent.ImageLabel.Visible = true
script.Parent.ImageLabel.LocalScript.Disabled = false
script.Parent.ImageLabel.LocalScript.attack:Play()
script.Parent.Parent.bgm.Volume = 0.3
script.Parent.Parent.bgm:Play()
end
else
if toggle == true then
toggle = false
script.Parent.ImageLabel.LocalScript.Disabled = true
script.Parent.ImageLabel.LocalScript.Thud:Stop()
for i = 0.6, 1, 0.02 do
script.Parent.ImageLabel.ImageTransparency = i
script.Parent.Parent.bgm.Volume = script.Parent.Parent.bgm.Volume - 0.005
wait()
end
script.Parent.Parent.bgm.Volume = 0
end
end
end
wait(.1)
end
local plrname = game.Players.LocalPlayer.Character.Name
local ifchased = game.Workspace.monsterchasewho:GetChildren()
local toggle = false
while true do
for i, monster in pairs(ifchased) do
if monster.Value == plrname then
if toggle == false then
toggle = true
script.Parent.ImageLabel.Visible = true
script.Parent.ImageLabel.LocalScript.Disabled = false
script.Parent.ImageLabel.LocalScript.attack:Play()
script.Parent.Parent.bgm.Volume = 0.3
script.Parent.Parent.bgm:Play()
break
end
else
if toggle == true then
toggle = false
script.Parent.ImageLabel.LocalScript.Disabled = true
script.Parent.ImageLabel.LocalScript.Thud:Stop()
for i = 0.6, 1, 0.02 do
script.Parent.ImageLabel.ImageTransparency = i
script.Parent.Parent.bgm.Volume = script.Parent.Parent.bgm.Volume - 0.005
wait()
end
script.Parent.Parent.bgm.Volume = 0
end
end
end
wait(.1)
end
-- input types: table, function, any
function CheckForOneValue(table, functorun, ...) -- ... is a multi argument thing, think of it as
-- input however many arguments as you want and you don't need to add
-- another argument to the function for each one
-- Print is one of these functions, you can input any number of things to print and it will output them all
for i,v in pairs(table) do
if v == true then -- can be converted to a "truthy" match by just having if v then
return functorun(...) -- running the function if the table has a true value
-- returning automatically breaks the loop, breaking might be more applicable in your case
end
end
end
This code is just to get you started in the general direction, I wouldn’t recommend copypasting it, but it will work just fine if you choose to do that.
Nah, basically it’s to check if an enemy is chasing you. If the enemy is chasing you it creates an effect on your screen, which kinda makes the situation more tense. Hopefully that helps.
The reason why a break in the script won’t work is because, even though the script breaks at the true value, there are still false values it scans before it reaches the true value, if that makes sense.
To be more specific, a monster chases the closest player. A script inside the monster will set the string value to the closest player’s name. There are many string values, since there are many monsters (about 10)
Inside the player’s gui, the script (The one I’ve been having issues with) is constantly checking if any of the string values contain the players name. If this is true, then it enables the GUI.
Each stringvalue is basically assigned to each monster, so the GUI script KNOWS which monster is chasing the player.
This is the approach I found for achieving this, although its possible that this could be an inefficient way of achieving this. Sorry for the misunderstanding
Im very new into roblox engine, and I didnt tested all the possibilities I would wish… So… just guessing.
There’s no way to attach a big not visible sphere to the monster or the player, so it can read the collision while getting close each other, in order to trigger the GUI script? I don’t even know if collision spheres exist as in other engines… but, that would be my first approach. So I dont have to constantly checking from client if a table says true or false about the monster proximity.
If theres no way to achieve somekind of collision range spheres… then… idk… I would make tests using ur approach. If someone could clarify somekind of system to achieve what you are looking for, would be helpful. Maybe today I will learn about this proximity topic, seems interesting :3
Ah, I see. I’m actually doing something similar to your sphere collision detector, except calculating the distance from the player and the monster.
The thing is, I’ve already done that, ahha. When the player gets close enough, the script changes the string value to that players name, also taking into the fact of which player is closest to the monster.
Hello! I believe you just need to rethink what you want the for loop to actually return. Since you are only scanning the monster table for just any instance of a value holding the players name I’ve assigned it to a table that exists only in the scope of that while loop. What happens is that after the for loop is ran, if it detects any instance of plrname it assigns it to a table called found_player. If found_player, toggle UI effect to true, else, toggle UI effect to false. @nicopattylegs
local plrname = game.Players.LocalPlayer.Character.Name
local ifchased = game.Workspace.monsterchasewho:GetChildren()
local toggle = false
while true do
local found_player = {}
for _, monster in pairs(ifchased) do
if monster.Value == plrname then
found_player[plrname] = plrname
end
end
if found_player[plrname] then
toggle = true
script.Parent.ImageLabel.Visible = true
script.Parent.ImageLabel.LocalScript.Disabled = false
script.Parent.ImageLabel.LocalScript.attack:Play()
script.Parent.Parent.bgm.Volume = 0.3
script.Parent.Parent.bgm:Play()
else
toggle = false
script.Parent.ImageLabel.LocalScript.Disabled = true
script.Parent.ImageLabel.LocalScript.Thud:Stop()
for i = 0.6, 1, 0.02 do
script.Parent.ImageLabel.ImageTransparency = i
script.Parent.Parent.bgm.Volume = script.Parent.Parent.bgm.Volume - 0.005
wait()
end
script.Parent.Parent.bgm.Volume = 0
end
wait(.1)
end
Actually reviewing my edit of the code, I’m not sure if the toggle value is necessary for debounce since the table will handle that, but I hope it helps a little