Detect region3 exit

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayMusic = ReplicatedStorage.RemoteEvents.FireClient.PlayMusic
local StopMusic = ReplicatedStorage.RemoteEvents.FireClient.StopMusic

local Position1 = script.Parent.Position1
local Position2 = script.Parent.Position2

local region = Region3.new(Position1.Position,Position2.Position)
local ignorelist = {Position2,Position1}

while wait(2) do
	local parts = workspace:FindPartsInRegion3WithIgnoreList(region,ignorelist,math.huge)
	for i,part in pairs(parts) do
		if part:IsA("Part") and part.Name == ("HumanoidRootPart") then
			local playername = part.Parent.Name
			local player = game.Players:FindFirstChild(playername)
			
			if player then
				print("Played stepped in region")
				PlayMusic:FireClient(player,"UndergroundArena")
			end
		end
	end
end

i got no idea how to detech when the player leaves the region3 any ideas

Well if the part.Name isnt HumanoidRootPart then that means the player isn’t in the region, is that what you mean??

while wait(2) do
	local parts = workspace:FindPartsInRegion3WithIgnoreList(region,ignorelist,math.huge)
	for i,part in pairs(parts) do
		if part:IsA("Part") and part.Name == ("HumanoidRootPart") then
			local playername = part.Parent.Name
			local player = game.Players:FindFirstChild(playername)
			
			if player then
				print("Played stepped in region")
				PlayMusic:FireClient(player,"UndergroundArena")
			end
        else
            print("Player is not in region??")
		end
	end
end

how would that work if there is multiple people with the same humanoidrootparts

If its a server script you could always turn it into a local script

if i did that wouldnt i have to use .touched

you could run through all the players and if the parent of the part is the players character, then send the remote to that specific player

u might better use GetTouchingParts() since it detects all the parts which are touching it very easily

Here is the code:

local Regions = workspace.Regions

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")

local function GetTouchingParts(part)
	local connection = part.Touched:Connect(function() end)
	local results = part:GetTouchingParts()
	connection:Disconnect()
	return results
end

game:GetService("RunService").Heartbeat:Connect(function()
	local TouchedParts = GetTouchingParts(HRP)

	local IsInRegion = false

	for _,hit in pairs(TouchedParts) do
		if hit.Parent == Regions then
			IsInRegion = true
		end
	end

	if IsInRegion == false then
		print("Isnt in region")
	else
		print("Is in region")
	end
end)

I prefer using parts for region

thats a complicated script for a simple thing lol

local Watching = {}
script.Parent.Touched:Connect(function(hit)
	if hit.Name ~= "HumanoidRootPart" then return end
	if table.find(Watching, hit.Parent) ~= nil then return end
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		table.insert(Watching, hit.Parent)
		local Index = #Watching
		local Connections = {}
		local function Cleanup()
			for i = 1, #Connections do
				Connections[i]:Disconnect()
			end
			table.remove(Watching, Index)
		end
		table.insert(Connections, Player.CharacterAdded:Connect(Cleanup))
		table.insert(Connections, hit.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
			if hit.Parent.Parent == nil then
				Cleanup()
			end
		end))
		table.insert(Connections, game.Players.PlayerRemoving:Connect(function(Plyr)
			if Plyr == Player then
				Cleanup()
			end
		end))
		print(Player.Name.." entered")
	end
end)
script.Parent.TouchEnded:Connect(function(hit)
	if hit.Name ~= "HumanoidRootPart" then return end
	local found = table.find(Watching, hit.Parent)
	if found ~= nil then
		table.remove(Watching, found)
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Player then
			print(Player.Name.." left")
		end
	end
end)

Zone.rbxl (22.1 KB)

1 Like

im just gun make music running all the time and u can choose to shut it off to lazy for htis

you could have a repeat wait until getRegionPartsFunction() == true

Inside the function make it return true if there is a humanoid or something in it