Im working on my game, and it has music regions. However, I got immense lag suddenly. I deleted this script and found out it was lagging me. I dont know why, or how it is causing unplayable lag, but music regions is a big part of a game. Does anyone know why or how this is happening? If so, thanks.
This is the code.
local plr = game.Players.LocalPlayer
local soundRegions = workspace:WaitForChild("SoundRegions")
soundRegions = soundRegions:GetChildren()
local soundManagement = {}
for _,region in pairs(soundRegions) do
local info = {}
local region3 = Region3.new(region.Position-(region.Size/2),region.Position+(region.Size/2))
region.Transparency = 1
info.Region = region3
info.Sound = script.SoundRegions:FindFirstChild(region.name).Sound
table.insert(soundManagement,info)
end
game:GetService("RunService").RenderStepped:Connect(function()
for _,soundInfo in pairs(soundManagement) do
local region = soundInfo.Region
local sound = soundInfo.Sound
local parts = workspace:FindPartsInRegion3WithWhiteList(region,plr.Character:GetDescendants())
if #parts > 0 then
if not sound.IsPlaying then
sound:Play()
end
else
sound:Stop()
end
end
end)

2 comments
Thank you! This helps alot.
You're welcome, i'm happy to help