-
What do you want to achieve?
Sound regions that loop music. -
What is the issue?
sound regions causing memory leak, and sounds wont loop
-
What solutions have you tried so far?
-Adding overlap parrams
-Using destroy rather than Debris
Sounds overlap when leaving one region to another really quickly. I suspect bad garbage collection on our part. Help would be greatly appreciated!
-- Services
local RunService = game:GetService("RunService")
local PlayersSv = game:GetService("Players")
local SoundService = game:GetService("SoundService")
local RunService = game:GetService("RunService")
local MarketPlaceService = game:GetService("MarketplaceService")
-- Assets
local Player = PlayersSv.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local MusicRegionsFolder = game.Workspace:WaitForChild("MusicRegions")
local GamepassRequiredFolder = game.Workspace:WaitForChild("GamepassRequiredREGION")
local PlayedTimeRequiredFolder = game.Workspace:WaitForChild("LockedByPlaytime")
-- Variables
local CurrentlyPlayingSoundId
local IsPlayerInRegion = false
--parrams
local TouchParams = OverlapParams.new()
TouchParams.FilterDescendantsInstances = {Player.Character:WaitForChild("HumanoidRootPart")}
TouchParams.FilterType = Enum.RaycastFilterType.Whitelist
while true do
wait(1)
for _, TimeRegion in PlayedTimeRequiredFolder:GetChildren() do
local TimeRequiredTouchingParts = game.Workspace:GetPartsInPart(TimeRegion, TouchParams)
for _, Part in TimeRequiredTouchingParts do
local Model = Part:FindFirstAncestorWhichIsA("Model")
if Model and Model == Character then
local TimePlayedValue = game.ReplicatedStorage.GetTimeValue:InvokeServer(Player)
if TimePlayedValue <= tonumber(TimeRegion.Name) then
Character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(workspace:WaitForChild("SpawnLocation").Position)
end
end
end
end
for _, GamepassRegion in GamepassRequiredFolder:GetChildren() do
local GamepassTouchingParts = game.Workspace:GetPartsInPart(GamepassRegion, TouchParams)
for _, Part in GamepassTouchingParts do
local Model = Part:FindFirstAncestorWhichIsA("Model")
if Model and Model == Character then
local GamepassSuccess, GamepassErrormessage = pcall(function()
MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, GamepassRegion.Name)
end)
if GamepassSuccess then
else
warn(GamepassErrormessage)
MarketPlaceService:PromptGamePassPurchase(Player, GamepassRegion.Name)
Character.HumanoidRootPart.CFrame = CFrame.new(workspace:WaitForChild("SpawnLocation").Position)
end
end
end
end
--Music Regions (need assistance)
for _, MusicRegion in MusicRegionsFolder:GetChildren() do
local MusicTouchingParts = game.Workspace:GetPartsInPart(MusicRegion, TouchParams)
for _, Part in MusicTouchingParts do
local Model = Part:FindFirstAncestorWhichIsA("Model")
if Model and Model == Character then
local RegionSound = MusicRegion:FindFirstChild("Music")
local NewSound = Instance.new("Sound")
NewSound.SoundId = RegionSound.SoundId
NewSound.Parent = script
if CurrentlyPlayingSoundId == nil then
CurrentlyPlayingSoundId = RegionSound.SoundId
NewSound:Play()
elseif CurrentlyPlayingSoundId ~= RegionSound.SoundId then
game.Debris:AddItem(script:FindFirstChild("Sound"), 0)
CurrentlyPlayingSoundId = RegionSound.SoundId
NewSound:Play()
end
IsPlayerInRegion = true
end
end
end
if not IsPlayerInRegion then
local Sound = script:FindFirstChild("Sound")
if Sound then game.Debris:AddItem(Sound, 0) end
end
IsPlayerInRegion = false
end