Problem using region3 (while loop, with system on and off)

Hello everyone,

It’s been several weeks that I’m struggling to solve a problem with a system for my Obby.
Here is my last topic (Problem with TouchedEvent when the character don't move - #5 by valtkins) concretely I have a system that activates and deactivates every X seconds, so if the player is already in the zone where the .Touched event is launched then it does not activate (if the player does not move).

I’m trying to get the same result but without the region3 issue.
The only problem is that to check with a region3 it is necessary to use a while loop but I use it so I cannot integrate it at the same time as the region3.

Here are the “contrainters” and “rules” to create a script to help me…
-There are 2 groups, each containing 2 parts.
-When the player touches a zone he dies.
-Group 1 and 2 are activated in a staggered way (e.g. when group 1 works, group 2 has no effect)
-With these parts we will create a region3.
-The activation of the zones changes every 5 seconds

Hierarchy of the parts:
image
Image of the parts (Yellow is groups 1 and red is groups 2):
image
Current script, i tryed to rewrite it like 5 times, but every times i get blocked with the region3 (for checking the player in the zone):

print("Iniatialization...")
local objectFolder = game.Workspace.Region3
local group1 = {}
local group2 = {}

local region3Zones_fans1_region = {}
local region3Zones_fans1_reference = {}
local region3Zones_fans2_region = {}
local region3Zones_fans2_reference = {}

local resultOfRegion3 = {}

local function effect() 
	print("Touched") --or kill player
end

for k, v in pairs(objectFolder:GetChildren()) do
	if v.Name == "Groups1" then
		table.insert(group1,#group1+1,v)
	elseif v.Name == "Groups2" then
		table.insert(group2,#group2+1,v)
	end
end

for k, v in pairs(group1) do
	local reference = v
	local minRegion3 = reference.Position - (0.5 * reference.Size)
	local maxRegion3 = reference.Position + (0.5 * reference.Size)
	local region3 = Region3.new(minRegion3,maxRegion3)
	
	table.insert(region3Zones_fans1_region,#region3Zones_fans1_region + 1, region3)
	table.insert(region3Zones_fans1_reference,#region3Zones_fans1_reference + 1, reference)
end
for k, v in pairs(group2) do
	local reference = v
	local minRegion3 = reference.Position - (0.5 * reference.Size)
	local maxRegion3 = reference.Position + (0.5 * reference.Size)
	local region3 = Region3.new(minRegion3,maxRegion3)

	table.insert(region3Zones_fans2_region,#region3Zones_fans2_region + 1, region3)
	table.insert(region3Zones_fans2_reference,#region3Zones_fans2_reference + 1, reference)
end

wait(10)
print("Finished")

while true do
	
	resultOfRegion3 = workspace:FindPartsInRegion3WithIgnoreList(currRegion, {currReference}, math.huge)
	wait()
end

My problem is to use the while true loop, to check the results of region3, while I also use it to activate and deactivate my groups.

Huge Thanks.

I note the topic as solved because nobody has an anwser

I don’t understand this part, can you explain what you mean here?

1 Like

I hope I will rephrase this better.

Basically, to check if a player or something else is in a region3 you have to call a function all the time in a while do loop (to do the same thing as a .Touched event).
Except that the problem is that I use this loop to switch between my two groups which works in a staggered fashion.

So in my while true do I can’t put the method to check if someone is in the area and the system that changes the operation of my two engines.

If it can help

while true do
  --I have a system that change every 5 secondes my 2 groups (when group 1 works, group 2 has no effect)
  -- And in this 2 groups i have 2 region3 that check a zone and if the player is in it that push the player away.

 --In this while loop i have to check everytime for the region3 of the working group and also every 5 secondes change the activities of the groups.
end

If I understand correctly, maybe you should have two different loops for each task?

For example:

local ActivatedGroup = 1 -- Staggers between 1 and 2

--This first loop simply switches the activated group back and forth...
spawn(function()
    while true do
        if ActivatedGroup == 1 then
            ActivatedGroup = 2
        else
            ActivatedGroup = 1
        end

        wait(5) -- The activated group will switch every 5 seconds.
    end
end)

-- This second loop detects region3 input...
while true do
    if ActivatedGroup == 1 then
        -- Group1 is currently activated so check it for players & push them away...
    else
        -- Group2 is currently activated so check it for players & push them away...
    end

    wait()
end

Yes I think you correctly understand, i see the spawn() function, i don’t know his usage, i will read about it and I keep you up. Thanks for all because maybe you solve this issues.

Thanks it works !

Just need to do it with my code but it is what i actualy needed.