Looking to improve this script

im looking to improve this script, the script checks if the user camera is above or below 500 studs, if it is above 500 studs, it stops seagulls and ocean from playing and plays sky, otherwise vice-versa.

local cam = workspace.CurrentCamera
local seagulls = workspace.seagull
local oceansound = workspace.ocean
local skysound = workspace.sky
local player = game.Players.LocalPlayer
local char = workspace:WaitForChild(player.Name)

while true do
	wait()
	if cam.CFrame.Y < 500 then
		seagulls.Playing = true
		oceansound.Playing = true
		skysound.Playing = false
	elseif cam.CFrame.Y > 500 then
		seagulls.Playing = false
		oceansound.Playing = false
		skysound.Playing = true
	end
end
while true do task.wait()
    if cam.CFrame.Y ~= 500 then
        seagulls.Playing = cam.CFrame.Y < 500
        oceansound.Playing = cam.CFrame.Y < 500
        skysound.Playing = cam.CFrame.Y > 500
    end
end

shortened the while loop
also not sure if this is intentional on your part, but no code runs if Y is 500
so just in case I put that if statement there

local players = game:GetService("Players")

local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local cam = workspace.CurrentCamera
local seagulls = workspace.seagull
local oceansound = workspace.ocean
local skysound = workspace.sky

the character variable should be defined like this instead

new code
local players = game:GetService("Players")

local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local cam = workspace.CurrentCamera
local seagulls = workspace.seagull
local oceansound = workspace.ocean
local skysound = workspace.sky

while true do task.wait()
    if cam.CFrame.Y ~= 500 then
        seagulls.Playing = cam.CFrame.Y < 500
        oceansound.Playing = cam.CFrame.Y < 500
        skysound.Playing = cam.CFrame.Y > 500
    end
end
2 Likes

what if you were to switch .Playing to .Volume? how would the = cam.CFrame.Y parts be like, since there would already be an existing = for setting the sound volume?

local players = game:GetService("Players")

local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local cam = workspace.CurrentCamera
local seagulls = workspace.seagull
local oceansound = workspace.ocean
local skysound = workspace.sky

while true do task.wait()
    if cam.CFrame.Y ~= 500 then
        seagulls.Volume = cam.CFrame.Y < 500 and 0.5 or 0
        oceansound.Volume = cam.CFrame.Y < 500 and 0.5 or 0
        skysound.Volume = cam.CFrame.Y > 500 and 0.5 or 0
    end
end

this is how the volume property would work(assuming the default volume is 0.5)

though changing the volume will not pause the song at all

1 Like

If you wanted it to fade depending on how far up the camera is, what you can do is:

--remove the player, character, and players variables because they are used no where in the script
local cam = workspace.CurrentCamera
local seagulls = workspace.seagull
local oceansound = workspace.ocean
local skysound = workspace.sky
local runs = game:GetService("RunService")

runs.Heartbeat:Connect(function()
local y = cam.CFrame.Y
local vol = math.clamp(0.5 - math.clamp(0,y/1000),0,0.5)
seagulls.Volume, oceansound.Volume, skysound.Volume = vol,vol,vol
end)

Haven’t tested it because roblox studio is down

I wouldn’t use a frame by frame check for this. It’s a waste of system resources imo.

Instead wait for the Camera’s CFrame to be changed or set the wait time to 5 or more seconds.

Also, depending on the style of the game I find it a lot more pleasant to have a fade in and fade out when entering zones.

This is the code I have used for switching between ambient noises and soundtracks

local FadeInformation = TweenInfo.new()
local FadeInGoal = {
    Volume = 0.5
}
local FadeOutGoal = {
    Volume = 0
}

local function FadeAmbient(oldSound, newSound)
    local FadeOut = TweenService:Create(oldSound, FadeInformation, FadeOutGoal)
    local FadeIn = TweenService:Create(newSound, FadeInformation, FadeInGoal)
    FadeOut:Play()
    FadeIn:Play()
    FadeOut.Completed:Wait()
    FadeOut:Destroy()
    FadeIn:Destroy()
end
1 Like