Region3 with whitelist not detecting players

I am having problems with region3 ONLY detecting players. I need my region3 to detect players and nothing else, otherwise it will be super laggy (due to mnay parts within the area).

here is my little snippet of code.

local Point1 =  script.Parent.Game0.Position
local Point2 = script.Parent.Game1.Position
local region = Region3.new(
    Vector3.new(math.min(Point1.X, Point2.X), math.min(Point1.Y, Point2.Y), math.min(Point1.Z, Point2.Z)),
    Vector3.new(math.max(Point1.X, Point2.X), math.max(Point1.Y, Point2.Y), math.max(Point1.Z, Point2.Z))

)

local lighting = game:GetService("Lighting")



local whitelist={}


while wait() do

   if #game.Players:GetChildren() > 0 then
	    for i,v in pairs(game.Players:GetChildren()) do
		    if game.Workspace:FindFirstChild(v) then
			    whitelist:insert(game.Workspace:FindFirstChild(v).HumanoidRootPart)
			
		    end
		
	    end
    end




    local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region, whitelist)

    for _,part in pairs(partsInRegion) do
	
	    if part.Parent:FindFirstChild("Humanoid") then
		    local plr = game.Players:FindFirstChild(part.Parent.Name)
		    local lighting = game:GetService("Lighting")
		    print(part.Parent)


		    minutes = lighting:GetMinutesAfterMidnight()
		    if plr then

		   	    if minutes >= 18 * 60 then
				    plr.LostForest:FireClient(plr)
				    plr.Character.Humanoid.Health = 0
			    elseif minutes >= 6 * 60 then
			    else
				    plr.LostForest:FireClient(plr)
				    plr.Character.Humanoid.Health = 0
			    end
		    end
	    end
     end

    local whitelist={}

end

the problem is in this specific part of your code; you are attempting to index the an array with an instance which is BAD.

instead just use v.Character or i guess you could stick with indexing with the player’s name but that’s kind of silly don’t you think

edit: colbert2677 answered the question way better than me

Out of curiosity, what’s your use case? I feel like there’s a better way you can do this and not have to worry about the Region3 not detecting players to begin with. There are several problems and nitpicks that I can see that I wouldn’t quite endorse for systems and even some errors. Please try and implement some basic debugging when your systems don’t work at the very least.

  • While wait is bad. Running expensive functions in one is not good either. If you need something to happen quickly then your code should also be acting fast. An example of how you can reduce the expensive per iteration is to get your player list from outside the loop.

  • Lack of use of GetService for services (stay consistent, use GetService to fetch services).

  • Use GetPlayers to get a list of players, not GetChildren. GetPlayers will return a list of Player instances and is the right function to use for getting players.

  • Use the Character property of Players if you need to check if they have characters. Don’t introduce the Workspace as a dependency for knowing if there’s a character in the Workspace or not. Additionally, instances that have the same name as a character can cause conflictions in your code. Finally, this doesn’t work as is because you’re passing an instance to look for when FindFirstChild takes a string, the name of the instance it should find.

  • Members of the table library do not get wrapped on tables automatically. You need to explicitly call table.insert if you want to add a member to a table. Calling it with colon syntax is invalid and will throw an error.

  • Use GetPlayerFromCharacter (this is after the FindPartsInRegion3WithWhiteList call is made) if you need the player instance from a part.

  • You can merge the conditions for the time check with an or instead of repeating the code with an elseif to differentiate each circumstance.

  • table.clear is a fast alternative for clearing the members of tables. I’d recommend using that over throwing away your old table and constructing a new one which will be allocated a size of 0 and be forced to resize again when the next iteration runs.

I’m not sure of what your exact use case is but it seems to be that you want to kill players if they’re in a certain region when the time is a specific value. Personally I would recommend just specifying your zone as a flat ground and using a raycast downwards every frame to check if the player is in your area instead of Region3, since raycasts can be executed very quickly.

2 Likes