How could I make a sound get louder or quiet when a player is closer or further from light sources?

But did you also try both the other methods for the sounds, as well as trying with & without streamingenabled in each method so you’d have an actual comparison?

I’m trying a lot right now, the best method is the one @Xapelize told me to use. It has the best performance so far. I will test a bit more with streamingenabled soon.

If I figure out a complete solution, I will add a solution to this post with my code.

I found a solution, here is what I have! Please note that the volume doesn’t work but its primary function of it does.

local RS = game:GetService("RunService")

local MAX_DISTANCE = 100 --Unused
local MIN_DISTANCE = 5 --Unused
local RANGE = 50 

local lastTarget = nil --Gets the last light it found

local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local HRP = Char:WaitForChild("HumanoidRootPart")

local Camera = workspace.CurrentCamera

local sound = script:WaitForChild("Light")

local lastVolume = 0 --self explanitory
RS.Heartbeat:Connect(function() --Heartbeat
	local overlapParams = OverlapParams.new()
	overlapParams.FilterDescendantsInstances = {workspace:WaitForChild("Map")}
	overlapParams.FilterType = Enum.RaycastFilterType.Whitelist

	local lights = workspace:GetPartBoundsInRadius(Camera.CFrame.Position, RANGE, overlapParams) --Creates a "ray" that iterates through the whitelisted table that finds any parts that is in a certain magnitude Range.
	local closestMagnitude = math.huge --Gets the closest magnitude
	local closestLight = nil --Gets the closest Light
	for k, v in pairs(lights) do
		if v:IsA("BasePart") then
			local magnitude = (v.Position - Camera.CFrame.Position).Magnitude --Gets the position
			if magnitude < closestMagnitude then
				closestMagnitude = magnitude
				closestLight = v
			end
		end
	end
	if closestLight ~= lastTarget then
		lastTarget = closestLight
		lastVolume = closestMagnitude/RANGE --Changes the volume
	end
	local sound = closestLight:FindFirstChild("Sound")
	if sound then
		sound.Volume = lastVolume
	else
		if closestLight == nil then
			lastVolume = lastVolume - 0.1 --Chages volume
		end
	end
end)

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