So I’m using Region3 to play different sounds in different areas of the map.
To do this, I made a folder with the region parts, and just checked them in a while loop every 3 seconds for players. If they aren’t in any of the regions, then they are in the sea if your confused on that part of the script.
However a noticeable lag spike occurs every time it checks. Right now, there are only 2 region parts in the folder. Also, I removed some things and replaced them with comments.
The Script (Local Script):
local regionChecks = {
["Spawn"] = false,
["Blue Cove"] = false,
}
local function checkRegions()
for island, value in pairs(regionChecks) do
if value == true then
return true
end
end
return false
end
local function updateLabel(name)
local guiFunction = coroutine.create(function()
mapRegionGui.Enabled = true
mapLabel.Text = name
--Tweening Frame Here
wait(.9)
--Tweening Frame Here
wait(1)
--Tweening Frame Here
wait(1)
mapRegionGui.Enabled = false
end)
coroutine.resume(guiFunction)
end
while wait(3) do
for i, regionPart in pairs(regionFolder:GetChildren()) do--Looping through each region part
local mapName = regionPart.Name
local foundPlayer = false
local min = regionPart.Position - (regionPart.Size / 2)
local max = regionPart.Position + (regionPart.Size / 2)
local region = Region3.new(min, max)
local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region, character:GetDescendants(), math.huge)
for i, part in pairs(partsInRegion) do
if part:FindFirstAncestor(character.Name) then
foundPlayer = true
break
else
foundPlayer = false
break
end
end
if foundPlayer and currentLocation.Value ~= mapName then
--Fading the sound with tween service in a courotine
--Cloning a sound and playing it
updateLabel(currentLocation.Value)
elseif not foundPlayer and currentLocation.Value ~= "The Deep Sea" then
--Fading the sound with tween service in a couroutine
if checkRegions() == false then
currentLocation.Value = "The Deep Sea"
--Fading the sound with tween service in a couroutine
--Cloning a sound and playing it
updateLabel(currentLocation.Value)
end
end
end
end