What do you want to achieve?
Need a sound script that will be softly silence if you’re too far away from the sound.
the sound will return to normal volume if you’re coming close to the sound.
What is the issue?
It doesn’t become silence while i’m far away from the sound.
What solutions have you tried so far?
RollOffMaxDistance also working but it only use for player’s camera, which wasn’t what i wanted.
--This is a local script
local Players = game:GetService("Players").LocalPlayer
local Character = Players.Character
local MaxVolumeRange = 150
while task.wait(0.1) do
for _, child in pairs(script.Parent:GetDescendants()) do
if child:IsA("Sound") then
local MaxVolume = child:GetAttribute("MaxVolume")
if MaxVolume ~= nil then
local magnitude = (Character.HumanoidRootPart.Position - child.Parent.Position).Magnitude
child.Volume = (MaxVolume / 100) * (MaxVolumeRange / magnitude)
end
end
end
end
You can change the center used by RollOffMaxDistance with SoundService:SetListener(…).
SetListener should be used on the client. You can set the listener to the character with something like:
local humanoidRootPart -- Probably use a connection to CharacterAdded then get the RootPart of the new character to get this value
game:GetService("SoundService"):SetListener(Enum.ListenerType.ObjectPosition, humanoidRootPart)
You can also set it to CFrames/positions in addition to BaseParts and the camera.
local Run = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Players.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local Model = script.Parent
Run.RenderStepped:Connect(function()
for _, Child in pairs(Model:GetDescendants()) do
if Child:IsA("Sound") then
local Part = Child.Parent
local Distance = (HRP.Position - Part.Position).Magnitude
if Distance > 150 then
Child.Volume = 0
else
Child.Volume = (75 / Distance)
end
end
end
end)
Check that the distance is greater than 150 studs and then set the sound’s volume to 0 before using the distance in the formula to calculate what the sound’s volume should be.
I also opted to use the .RenderStepped event loop.