Hi, in this tutorial we are going to learn how to use sound PlaybackLoudness
with a camera and make visualizer effect.
What is PlayBackLoudness?
This property reflects the amplitude of the Sound
's playback in the instance of time it is read. Therefore, for most sounds it will fluctuate constantly. Due to this it can appear in the Roblox Studio properties window as 0, however when read by code in the command bar or Script
s it will return the correct value.
As said in the wiki Here.
Let’s get started, shall we?
We going to start up by adding sound.
The next step is to edit our soundId.
Now we got our sound ready let’s hop intro scripting!
We will start up by adding a local script and name it visualizer StarterPlayer > StarterCharacterScripts
.
We going to start up by adding variables.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
Now we want to use RunService and variables.
game:GetService("RunService").RenderStepped:Connect(function()
local MySound = workspace.Sound.PlaybackLoudness
local Humanoid = char.Humanoid
end)
Now let’s do some math!
Our player camera always looking at Z-axis now let’s move the part at the position the player looking at and let take the Z-axis and reverse it to match the camera.
As you can see the Z-axis is negative which means the camera is positive.
Let’s do coding time.
We are going to use cameraoffset to achieve this effect.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
game:GetService("RunService").RenderStepped:Connect(function()
local MySound = workspace.Sound.PlaybackLoudness
local Humanoid = char.Humanoid
Humanoid.CameraOffset = Vector3.new(0,MySound,MySound)
end)
After doing that you will notice there is an error as showing here.
So, how to fix that?
As said in the wiki: A number between 0 and 1000 indicating how loud the Sound is currently playing back.
We need to add numbers to fix this weird glitch.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
game:GetService("RunService").RenderStepped:Connect(function()
local MySound = workspace.Sound.PlaybackLoudness
local Humanoid = char.Humanoid
local Power = 400
Humanoid.CameraOffset = Vector3.new(0,MySound/Power,MySound/Power)
end)
So our power will be 400 let’s see if this will work out.
looks like it is working but it’s going to Y-axis as the same as Z-axis and it’s not smooth and cool, let’s fix that by adding superpower we will take our power and double it by 2x.
We will change the Y-axis only {x,y,z}
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
game:GetService("RunService").RenderStepped:Connect(function()
local MySound = workspace.Sound.PlaybackLoudness
local Humanoid = char.Humanoid
local SuperPower = 800
local Power = 400
Humanoid.CameraOffset = Vector3.new(0,MySound/SuperPower,MySound/Power)
end)
Now let’s check it out!
Look’s great and smooth we made it!
Note: You can mess around with numbers to make it more smooth.
The whole script
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
game:GetService("RunService").RenderStepped:Connect(function()
local MySound = workspace.Sound.PlaybackLoudness
local Humanoid = char.Humanoid
local SuperPower = 800
local Power = 400
Humanoid.CameraOffset = Vector3.new(0,MySound/SuperPower,MySound/Power)
end)
Thanks for following up my tutorial, I’m sorry for bad grammar and some mistake I’m Arabic.
Note: Sorry if this is wrong category I’m new to devfourm.