Lag when entering invisible block to trigger a script

You can write your topic however you want, but you need to answer these questions:
I’m trying to attain an area when you enter the area the sound reverbs, ex; driving through a tunnel in a police car the sirens go from normal to reverb (adds realism)

I got a “tunnel echo” from the toolbox it works, but when you enter the area the FPS drops for all players client sided.

How would I fix it from not lagging (or fps dropping) when entering the area.

Heres the script. The Tunnel area is an invisible block part. (The script goes into StarterPLayerScripts)

 -- Function to get all parts named TunnelArea and set them to transparent
 local function getTunnelAreas()
 	local tunnelAreas = {}
 	for _, part in ipairs(workspace:GetChildren()) do
 		if part:IsA("BasePart") and part.Name == "TunnelArea" then
 			-- Set transparency to 1 for each TunnelArea part
 			part.Transparency = 1
 			table.insert(tunnelAreas, part)
 			-- Ensure TouchInterest is created for each part
 			part.Touched:Connect(function() end)
 		end
 	end
 	return tunnelAreas
 end
 
 -- Function to check if the player is in any TunnelArea
 local function isPlayerInAnyArea(player)
 	local character = player.Character
 	if not character then return false end
 
 	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
 	if not humanoidRootPart then return false end
 
 	local tunnelAreas = getTunnelAreas()
 	for _, part in ipairs(tunnelAreas) do
 		local touching = part:GetTouchingParts()
 		for _, touchPart in ipairs(touching) do
 			if touchPart == humanoidRootPart then
 				return true
 			end
 		end
 	end
 	return false
 end
 
 -- Main loop to check if the player is in any TunnelArea
 game:GetService("RunService").RenderStepped:Connect(function()
 	local player = game.Players.LocalPlayer
 	if isPlayerInAnyArea(player) then
 		game.SoundService.AmbientReverb = "ConcertHall"
 	else
 		game.SoundService.AmbientReverb = "NoReverb"
 	end
 end) ```

Please put 3 backticks (```) before and after your copy/pasted script so it formats properly.

How many TunnelAreas are there?

I’m pretty sure you don’t need to set up TouchInterest in every tunnel Part. Just make sure the CanTouch property is manually set to true for all your TunnelAreas.

This script is checking every RenderStep (you’re also reassigning player when it only needs to be set once at the beginning of the script) through the entire table of TunnelAreas and through every Part in the Player model. That’s going to cause lag in your game.
Try checking every .2 seconds or so instead of RenderStepped and only checking for . Having the sound change .2 seconds ‘late’ shouldn’t make as much of a difference.

Anyways, here’s a better option. In this link for the SoundService documentation they give you the actual script to perform this reverb change and it looks much more efficient than the tunnel echo you got from the toolbox.

1 Like

Can you try that and tell me if it work? It is a LocalScript in StarterCharacterScript.

local SoundService = game:GetService("SoundService")

local Character = script.Parent
local Root = Character:WaitForChild("HumanoidRootPart")

local Touched = nil

-- Functions to connect/disconnect the touch event on tunnels
local function ConnectTouched(EnteredInTunnel, WaitToEnterTunnel)
	Touched = Root.Touched:Connect(function(TouchedPart)
		if TouchedPart.Name == "TunnelArea" then
			EnteredInTunnel(WaitToEnterTunnel)
		end
	end)
end

local function DisconnectTouched()
	if Touched then
		Touched:Disconnect()
		Touched = nil
	end
end

-- Functions to check when player is or not in a tunnel
local function WaitToLeaveTunnel(WaitToEnterTunnel)
	local IsInTunnel = nil

	repeat task.wait(0.25)
		IsInTunnel = false
		for _, Part in workspace:GetPartsInPart(Root) do
			if Part.Name == "TunnelArea" then
				IsInTunnel = true
			end
		end
	until IsInTunnel == false

	WaitToEnterTunnel()
end

local function EnteredInTunnel(WaitToEnterTunnel)
	DisconnectTouched()
	SoundService.AmbientReverb = Enum.ReverbType.ConcertHall
	WaitToLeaveTunnel(WaitToEnterTunnel)
end

local function WaitToEnterTunnel()
	ConnectTouched(EnteredInTunnel, WaitToEnterTunnel)
	SoundService.AmbientReverb = Enum.ReverbType.NoReverb
end

-- Starting Script
WaitToEnterTunnel()
1 Like

This script worked there’s no noticeable lag at all on my end thankyou.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.