Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I tell when a player has left a Region3?

I'm just now learning about Region3's because I think it's good for what I'm trying to do. I basically am trying to make a safe zone, where players inside the region will have a ForceField and players outside of it will not. I have found out how to make players inside it have a ForceField, but I'm not sure how to make their ForceField go away when they leave the region. Any help?

local part = script.Parent
local region = Region3.new(part.Position - (part.Size / 2), part.Position + (part.Size / 2))

while true do
	local partsinregion = workspace:FindPartsInRegion3(region, part)
	
	for _, part in pairs(partsinregion) do
		if part.Parent:FindFirstChild("Humanoid") then
			print("part belongs to player")
			
			if not part.Parent:FindFirstChild("ForceField") then
				local forcefield = Instance.new("ForceField")
				forcefield.Parent = part.Parent
			end
		end
	end
	
	wait()
end

Update: I just tried using the TouchEnded function but it did not work sadly!

3 answers

I have never done this with region3, but i might try to make 3 different table variables,

tableA for containing the parts in region3,

tableB for containing current list of player in region,

tableC for saving the old list of player from the previous iteration of the loop, this changes to be the same as tableB at the end of the loop or when its empty or nil

So how this goes is,

-Detect all the part in region3 put them in tableA using findpartsinregion3 function

-Go through the list of tableA, find player character by finding humanoid, and then find the player with GetPlayerFromCharacter function and put them in the tableB, and give all the characters there the forcefield

-Check if tableC is nil or empty, if nil or empty, just copy tableB and go into the next iteration of the loop

If tableC is not nil or empty, compare tableC and tableB, see which player exist in tableC and not in tableB, those that does not exist on tableB but exist on tableC get their forcefield removed (also checks if they are still in game or if the forcefield still exist).

Im just giving you idea on how this work, how you implement them is up to you.

Also im typing on mobile, excuse grammar and spelling mistakes

You can try by firstly using the region3 normally and checking if they're in the region3. Inside your if statement for when the player is inside the region3, try including an else statement which means they aren't inside. I haven't had much experience with region3 so I'm not sure if this will help you.

This discussion is preserved as it was. New answers, comments, and votes are closed.