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
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)
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)