How to detect player and a part in a region

local part = game.Workspace.SoundRegions.RedRegion

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

game:GetService("RunService").Stepped:Connect(function()
	for i,v in pairs(workspace:FindPartsInRegion3(region, part, math.huge)) do
		if v.Parent:FindFirstChild("Humanoid") then
			print('You are in region!: ', v.Parent:FindFirstChild("Humanoid").Parent.Name)
		end
	end
end)

How would I check if a part and the player is in the region at the same time I know?

I know I can do 2 loops to check ball and the player is in the region but i need something to happen when both are in the region

Any help is appreciated.

Sorry I misread, you would create two variables for if the player is in the region and if the part is in the region, and then create a function and fire it when both values are true.

1 Like

findpartsinregion3 is deprecated so you may want to take a look at

Also you may want to use GetPlayerFromCharacter which will make sure the part is a valid part of a players character

local Player = game.Players:GetPlayerFromCharacter(part.Parent)
1 Like

Thanks for that was very easy to use rather than FindPartsInRegion3

Sorry meant to reply to the other guy :joy:

Look into the ZonePlus module.

1 Like

Works brilliantly thank you much easier than scripting the whole thing out

You were on the right track.

local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local RunService = Game:GetService("RunService")
local RegionFolder = Workspace.Regions --Folder of regions.
local Regions = RegionFolder:GetChildren() --Region folder's children (regions).

local function OnHeartbeat()
	for _, Region in ipairs(Regions) do
		local Parts = Workspace:GetPartsInPart(Region)
		for _, Part in ipairs(Parts) do
			local Model = Part:FindFirstAncestorOfClass("Model")
			if not Model then continue end
			local Humanoid = Model:FindFirstChildOfClass("Humanoid")
			if not Humanoid then continue end
			local Player = Players:GetPlayerFromCharacter(Model)
			if not Player then continue end
			print(Player.Name.." is in "..Region.Name..".")
		end
	end
end

RunService.Heartbeat:Connect(OnHeartbeat)
1 Like

But how would I be able to check if the player and the model is in the region for it to trigger that’s my only issue I tried ZonePlus Module but it didn’t work the way it should’ve for 2 regions

Zoneplus module doesn’t work for me. I keep clicking the plus button but nothing happened. Not sure if it was a virus or not. Can somebody link me the real one?

Use the API link in the devforum to help you use it it is very simple to use

1 Like

-Alex

I’m going to give you an answer that is a bit more efficient than your original code.
Here is the code to find the region:
local part = game.Workspace.SoundRegions.RedRegion
local region = part.CFrame:ToWorldSpace(part.Size / -2) --Pos1
local region2 = part.CFrame:ToWorldSpace(part.Size / 2) --Pos2

Here is the code to find the players in the region:
for i,v in pairs(game.Players:GetChildren()) do
if v.Character then
if v.Character:FindFirstChild(“HumanoidRootPart”) then
local findPart = v.Character:FindFirstChild(“HumanoidRootPart”)
if findPart.Position.X > region.X and findPart.Position.X < region2.X and findPart.Position.Y > region.Y and findPart.Position.Y < region2.Y and findPart.Position.Z > region.Z and findPart.Position.Z < region2.Z then
–Put your code here
end
end
end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.