How to check one someone leaves a region3 or isnt in one

I have a value inside of the players character called “Floating” and when it’s set to True it makes the character float. I’m trying to make it so when the player is in the set region they float which I have but I’m not sure how to make so if the player leaves the area they stop floating (so setting the value to false)

while wait(1) do
    local Region = Region3.new(script.Parent.Position - script.Parent.Size / 2, script.Parent.Position + script.Parent.Size / 2)
    local IgnoreList = {script.Parent, game.Workspace.Map, game.Workspace:WaitForChild("WaterFloor")}
    local PartsInRegion = game.Workspace:FindPartsInRegion3WithIgnoreList(Region, IgnoreList, math.huge)
    for i,v in pairs(PartsInRegion) do
        if v  then
            if v.Name == "HumanoidRootPart" then
                v.Parent.Float.Value = "True"
            end
        end
    end
end
3 Likes

Try this:

while wait(1) do
	for Index,Player in pairs(game.Players:GetPlayers()) do
		if Player.Character~=nil then
			local Region = Region3.new(script.Parent.Position - script.Parent.Size / 2, script.Parent.Position + script.Parent.Size / 2)
			local PartsInRegion = game.Workspace:FindPartsInRegion3WithWhiteList(Region,Player.Character:GetDescendants())

			if #PartsInRegion > 0 then
				if Player.Character.Float.Value ~= "True" then
					Player.Character.Float.Value = "True"
				end
			else
				if Player.Character.Float.Value ~= "False" then
					Player.Character.Float.Value = "False"
				end
			end
		end
	end
end

This should give this result:

But if you only use the “True” and “False” values, I would recommend using boolValue rather than stringValue:

if #PartsInRegion > 0 then
	if Player.Character.Float.Value == false then
		Player.Character.Float.Value = true
	end
else
	if Player.Character.Float.Value == true then
		Player.Character.Float.Value = false
	end
end
2 Likes

That could just be condensed to this:

Player.Character.Float.Value = #PartsInRegion > 0

There’s no need to check the value; just set it to what it should be.

RunService is a service which uses something called RenderStepped or Stepped if you’re running this on the client, use RenderStepped if not, use Stepped . These run every frame, if you’re wondering, no this wouldn’t lag.

I know another tactic that does not use Region3!

workspace:GetPartsInPart() will return all parts in a part. You can make the part transparent and not-collidable, and then check if it is a player.

This is a script I have that detectes player in a part.

The table AlreadyCounted is the list of the players in a box. PartsInArea is from workspace:GetPartsInPart(part).

for i,v in pairs(PartsInArea) do
		local PossibleChar = v.Parent
		if PossibleChar then if PossibleChar:FindFirstChild("Humanoid") then
			local Player = game.Players:WaitForChild(PossibleChar.Name,2)
			if Player then
				if not table.find(AlreadyCounted,Player.UserId) then
					local PlrID = Player.UserId
					local ToSend = {}
					ToSend.PlrId = PlrID
					ToSend.Type = "Xp"
					ToSend.Value = 10
					table.insert(TotalToSend,ToSend)
					table.insert(AlreadyCounted,Player.UserId)
				end
			end end
		end

I hope this helped!

To find the players not in the part, simply loop through the players and check if their ID is in the AlreadyCounted or whatever you would name your table.

1 Like