Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Screen shakes only for one song?

i am trying to make a script witch would shake your screen with the music but the script works only for one song.

local RunService = game:GetService("RunService")

local Music = workspace.Sounds.Music
local Music1 = workspace.Sounds.Music1
local Music2 = workspace.Sounds.Music2
local Music3 = workspace.Sounds.Music3
local Music4 = workspace.Sounds.Music4
local Music5 = workspace.Sounds.Music5
local Music6 = workspace.Sounds.Music6
local Music7 = workspace.Sounds.Music7
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
	CameraMinFOV = 40,
	CameraMaxFOV = 50,
	CameraMaxVolume = 1200
}


game:GetService("RunService").RenderStepped:connect(function()
	local CurrentLoudness = Music.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end)
game:GetService("RunService").RenderStepped:connect(function()
	local CurrentLoudness = Music1.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end)
game:GetService("RunService").RenderStepped:connect(function()
	local CurrentLoudness = Music2.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end)
game:GetService("RunService").RenderStepped:connect(function()
	local CurrentLoudness = Music3.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end)game:GetService("RunService").RenderStepped:connect(function()
	local CurrentLoudness = Music4.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end)
game:GetService("RunService").RenderStepped:connect(function()
	local CurrentLoudness = Music5.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end)
game:GetService("RunService").RenderStepped:connect(function()
	local CurrentLoudness = Music6.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end)
game:GetService("RunService").RenderStepped:connect(function()
	local CurrentLoudness = Music7.PlaybackLoudness
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	end
end)

1 answer

You dont need to keep on doing the same concept of game:GetService('RunService') etc

you can just do it in one function and use else statements. Also you already have runservice as a variable so idk why u mention it again.

Here is an example

local runService = game:GetService('RunService')

local Music = workspace.Sounds.Music
local Music1 = workspace.Sounds.Music1
local Music2 = workspace.Sounds.Music2
local Music3 = workspace.Sounds.Music3
local Music4 = workspace.Sounds.Music4
local Music5 = workspace.Sounds.Music5
local Music6 = workspace.Sounds.Music6
local Music7 = workspace.Sounds.Music7
local CurrentCamera = workspace.CurrentCamera

local ScreenShakeSettings = {
    CameraMinFOV = 40,
    CameraMaxFOV = 50,
    CameraMaxVolume = 1200
}

runService.RenderStopped:Connect(function()
	local currentLoudness = Music.PlaybackLoudness -- I also suggest you use different vars
	local FOV = ScreenShakeSettings.CameraMinFOV + (ScreenShakeSettings.CameraMaxFOV - ScreenShakeSettings.CameraMinFOV) * (CurrentLoudness / ScreenShakeSettings.CameraMaxVolume)
	if FOV > 0 and FOV < 130 then
		CurrentCamera.FieldOfView = FOV
	elseif FOV > 0 and FOV < 130 then
			CurrentCamera.FieldOfView = FOV
	--Thats where the next elseif comes
		end
	end
end)

I also advise that you add different arguments to your if statement (if FOV > 0 and FOV <130 as thats probably why the error is occurring. Maybe try and do this.

local musicEnd = false

if musicEnd == true then
	--Execute function
end

This discussion is preserved as it was. New answers, comments, and votes are closed.