I’m new to coroutines and I’m trying to use region 3 and coroutines in a for loop. My Problem is the for loop only triggers the coroutine once and none of the other parts
This Example script gets the parts in workspace.Part(folder) and repeatedly checks if a player in the area.
local CheckRegion = coroutine.create(function(region,MPart)
while wait(0.1) do--Keeps Checking
local partsInRegion = game.Workspace:FindPartsInRegion3(region,nil,math.huge)
for _, part in pairs(partsInRegion) do
local plyr = Players:GetPlayerFromCharacter(part.Parent)
if plyr then
print("player in region")
end
end
end
end)
for i,MPart in pairs(workspace.Parts:GetChildren()) do
local pos1 = part.Position - (part.Size / 2)
local pos2 = part.Position + (part.Size / 2)
local region = Region3.new(pos1, pos2)
coroutine.resume(CheckRegion,region,MPart)
end
function CheckRegion(region,MPart)
while wait(0.1) do--Keeps Checking
local partsInRegion = game.Workspace:FindPartsInRegion3(region,nil,math.huge)
for _, part in pairs(partsInRegion) do
local plyr = Players:GetPlayerFromCharacter(part.Parent)
if plyr then
print("player in region")
end
end
end
end
for i,MPart in pairs(workspace.Parts:GetChildren()) do
local pos1 = part.Position - (part.Size / 2)
local pos2 = part.Position + (part.Size / 2)
local region = Region3.new(pos1, pos2)
coroutine.wrap(CheckRegion)(region,MPart)
end
How many Region 3s do you need to check that often and why? I know it’s to check players in an area, but how big of area(s) are you checking and why does it need to check that often? It seems there might be a better solution available.
Its basically a lot and property function, lots (construction area lot, not many) spawn in the game and it use region 3 to check when players are in those lots.