Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Thank SirHurt For Remote.
- local SFX = {
- Died = 0,
- Running = 1,
- Swimming = 2,
- Climbing = 3,
- Jumping = 4,
- GettingUp = 5,
- FreeFalling = 6,
- FallingDown = 7,
- Landing = 8,
- Splash = 9
- }
- local Humanoid, Head
- local Sounds = {}
- local SoundService = game:GetService("SoundService")
- local soundEventFolderName = "DefaultSoundEvents"
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local AddCharacterLoadedEvent, RemoveCharacterEvent
- local soundEventFolder = ReplicatedStorage:FindFirstChild(soundEventFolderName)
- if not soundEventFolder then
- soundEventFolder = Instance.new("Folder", ReplicatedStorage)
- soundEventFolder.Name = soundEventFolderName
- soundEventFolder.Archivable = false
- end
- RemoveCharacterEvent = soundEventFolder:FindFirstChild("RemoveCharacterEvent")
- if RemoveCharacterEvent == nil then
- RemoveCharacterEvent = Instance.new("RemoteEvent", soundEventFolder)
- RemoveCharacterEvent.Name = "RemoveCharacterEvent"
- end
- AddCharacterLoadedEvent = soundEventFolder:FindFirstChild("AddCharacterLoadedEvent")
- if AddCharacterLoadedEvent == nil then
- AddCharacterLoadedEvent = Instance.new("RemoteEvent", soundEventFolder)
- AddCharacterLoadedEvent.Name = "AddCharacterLoadedEvent"
- end
- AddCharacterLoadedEvent:FireServer()
- game.Players.LocalPlayer.CharacterRemoving:connect(function(character)
- RemoveCharacterEvent:FireServer(game.Players.LocalPlayer)
- end)
- do
- local Figure = script.Parent.Parent
- Head = Figure:WaitForChild("Head")
- while not Humanoid do
- for _, NewHumanoid in pairs(Figure:GetChildren()) do
- if NewHumanoid:IsA("Humanoid") then
- Humanoid = NewHumanoid
- break
- end
- end
- if Humanoid then
- break
- end
- Figure.ChildAdded:wait()
- end
- Sounds[SFX.Died] = Head:WaitForChild("Died")
- Sounds[SFX.Running] = Head:WaitForChild("Running")
- Sounds[SFX.Swimming] = Head:WaitForChild("Swimming")
- Sounds[SFX.Climbing] = Head:WaitForChild("Climbing")
- Sounds[SFX.Jumping] = Head:WaitForChild("Jumping")
- Sounds[SFX.GettingUp] = Head:WaitForChild("GettingUp")
- Sounds[SFX.FreeFalling] = Head:WaitForChild("FreeFalling")
- Sounds[SFX.Landing] = Head:WaitForChild("Landing")
- Sounds[SFX.Splash] = Head:WaitForChild("Splash")
- local DefaultServerSoundEvent = soundEventFolder:FindFirstChild("DefaultServerSoundEvent")
- if DefaultServerSoundEvent then
- DefaultServerSoundEvent.OnClientEvent:connect(function(sound, playing, resetPosition)
- if resetPosition and sound.TimePosition ~= 0 then
- sound.TimePosition = 0
- end
- if sound.IsPlaying ~= playing then
- sound.Playing = playing
- end
- end)
- end
- end
- local function IsSoundFilteringEnabled()
- return game.Workspace.FilteringEnabled and SoundService.RespectFilteringEnabled
- end
- local Util
- Util = {
- YForLineGivenXAndTwoPts = function(x, pt1x, pt1y, pt2x, pt2y)
- local m = (pt1y - pt2y) / (pt1x - pt2x)
- local b = pt1y - m * pt1x
- return m * x + b
- end,
- Clamp = function(val, min, max)
- return math.min(max, math.max(min, val))
- end,
- HorizontalSpeed = function(Head)
- local hVel = Head.Velocity + Vector3.new(0, -Head.Velocity.Y, 0)
- return hVel.magnitude
- end,
- VerticalSpeed = function(Head)
- return math.abs(Head.Velocity.Y)
- end,
- Play = function(sound)
- if IsSoundFilteringEnabled() then
- sound.CharacterSoundEvent:FireServer(true, true)
- end
- if sound.TimePosition ~= 0 then
- sound.TimePosition = 0
- end
- if not sound.IsPlaying then
- sound.Playing = true
- end
- end,
- Pause = function(sound)
- if IsSoundFilteringEnabled() then
- sound.CharacterSoundEvent:FireServer(false, false)
- end
- if sound.IsPlaying then
- sound.Playing = false
- end
- end,
- Resume = function(sound)
- if IsSoundFilteringEnabled() then
- sound.CharacterSoundEvent:FireServer(true, false)
- end
- if not sound.IsPlaying then
- sound.Playing = true
- end
- end,
- Stop = function(sound)
- if IsSoundFilteringEnabled() then
- sound.CharacterSoundEvent:FireServer(false, true)
- end
- if sound.IsPlaying then
- sound.Playing = false
- end
- if sound.TimePosition ~= 0 then
- sound.TimePosition = 0
- end
- end
- }
- do
- local playingLoopedSounds = {}
- local activeState
- local fallSpeed = 0
- function setSoundInPlayingLoopedSounds(sound)
- for i = 1, #playingLoopedSounds do
- if playingLoopedSounds[i] == sound then
- return
- end
- end
- table.insert(playingLoopedSounds, sound)
- end
- function stopPlayingLoopedSoundsExcept(except)
- for i = #playingLoopedSounds, 1, -1 do
- if playingLoopedSounds[i] ~= except then
- Util.Pause(playingLoopedSounds[i])
- table.remove(playingLoopedSounds, i)
- end
- end
- end
- local stateUpdateHandler = {
- [Enum.HumanoidStateType.Dead] = function()
- stopPlayingLoopedSoundsExcept()
- local sound = Sounds[SFX.Died]
- Util.Play(sound)
- end,
- [Enum.HumanoidStateType.RunningNoPhysics] = function(speed)
- stateUpdated(Enum.HumanoidStateType.Running, speed)
- end,
- [Enum.HumanoidStateType.Running] = function(speed)
- local sound = Sounds[SFX.Running]
- stopPlayingLoopedSoundsExcept(sound)
- if activeState == Enum.HumanoidStateType.Freefall and fallSpeed > 0.1 then
- local vol = math.min(1, math.max(0, (fallSpeed - 50) / 110))
- local freeFallSound = Sounds[SFX.FreeFalling]
- freeFallSound.Volume = vol
- Util.Play(freeFallSound)
- fallSpeed = 0
- end
- if speed ~= nil and speed > 0.5 then
- Util.Resume(sound)
- setSoundInPlayingLoopedSounds(sound)
- elseif speed ~= nil then
- stopPlayingLoopedSoundsExcept()
- end
- end,
- [Enum.HumanoidStateType.Swimming] = function(speed)
- local threshold = speed
- if activeState ~= Enum.HumanoidStateType.Swimming and threshold > 0.1 then
- local splashSound = Sounds[SFX.Splash]
- splashSound.Volume = Util.Clamp(Util.YForLineGivenXAndTwoPts(Util.VerticalSpeed(Head), 100, 0.28, 350, 1), 0, 1)
- Util.Play(splashSound)
- end
- local sound = Sounds[SFX.Swimming]
- stopPlayingLoopedSoundsExcept(sound)
- Util.Resume(sound)
- setSoundInPlayingLoopedSounds(sound)
- end,
- [Enum.HumanoidStateType.Climbing] = function(speed)
- local sound = Sounds[SFX.Climbing]
- if speed ~= nil and math.abs(speed) > 0.1 then
- Util.Resume(sound)
- stopPlayingLoopedSoundsExcept(sound)
- else
- Util.Pause(sound)
- stopPlayingLoopedSoundsExcept(sound)
- end
- setSoundInPlayingLoopedSounds(sound)
- end,
- [Enum.HumanoidStateType.Jumping] = function()
- if activeState == Enum.HumanoidStateType.Jumping then
- return
- end
- stopPlayingLoopedSoundsExcept()
- local sound = Sounds[SFX.Jumping]
- Util.Play(sound)
- end,
- [Enum.HumanoidStateType.GettingUp] = function()
- stopPlayingLoopedSoundsExcept()
- local sound = Sounds[SFX.GettingUp]
- Util.Play(sound)
- end,
- [Enum.HumanoidStateType.Freefall] = function()
- if activeState == Enum.HumanoidStateType.Freefall then
- return
- end
- local sound = Sounds[SFX.FreeFalling]
- sound.Volume = 0
- stopPlayingLoopedSoundsExcept()
- fallSpeed = math.max(fallSpeed, math.abs(Head.Velocity.y))
- end,
- [Enum.HumanoidStateType.FallingDown] = function()
- stopPlayingLoopedSoundsExcept()
- end,
- [Enum.HumanoidStateType.Landed] = function()
- stopPlayingLoopedSoundsExcept()
- if Util.VerticalSpeed(Head) > 75 then
- local landingSound = Sounds[SFX.Landing]
- landingSound.Volume = Util.Clamp(Util.YForLineGivenXAndTwoPts(Util.VerticalSpeed(Head), 50, 0, 100, 1), 0, 1)
- Util.Play(landingSound)
- end
- end,
- [Enum.HumanoidStateType.Seated] = function()
- stopPlayingLoopedSoundsExcept()
- end
- }
- function stateUpdated(state, speed)
- if stateUpdateHandler[state] ~= nil then
- if state == Enum.HumanoidStateType.Running or state == Enum.HumanoidStateType.Climbing or state == Enum.HumanoidStateType.Swimming or state == Enum.HumanoidStateType.RunningNoPhysics then
- stateUpdateHandler[state](speed)
- else
- stateUpdateHandler[state]()
- end
- end
- activeState = state
- end
- Humanoid.Died:connect(function()
- stateUpdated(Enum.HumanoidStateType.Dead)
- end)
- Humanoid.Running:connect(function(speed)
- stateUpdated(Enum.HumanoidStateType.Running, speed)
- end)
- Humanoid.Swimming:connect(function(speed)
- stateUpdated(Enum.HumanoidStateType.Swimming, speed)
- end)
- Humanoid.Climbing:connect(function(speed)
- stateUpdated(Enum.HumanoidStateType.Climbing, speed)
- end)
- Humanoid.Jumping:connect(function()
- stateUpdated(Enum.HumanoidStateType.Jumping)
- end)
- Humanoid.GettingUp:connect(function()
- stateUpdated(Enum.HumanoidStateType.GettingUp)
- end)
- Humanoid.FreeFalling:connect(function()
- stateUpdated(Enum.HumanoidStateType.Freefall)
- end)
- Humanoid.FallingDown:connect(function()
- stateUpdated(Enum.HumanoidStateType.FallingDown)
- end)
- Humanoid.StateChanged:connect(function(old, new)
- stateUpdated(new)
- end)
- function onUpdate(stepDeltaSeconds, tickSpeedSeconds)
- local stepScale = stepDeltaSeconds / tickSpeedSeconds
- do
- local sound = Sounds[SFX.FreeFalling]
- if activeState == Enum.HumanoidStateType.Freefall then
- if Head.Velocity.Y < 0 and Util.VerticalSpeed(Head) > 75 then
- Util.Resume(sound)
- local ANIMATION_LENGTH_SECONDS = 1.1
- local normalizedIncrement = tickSpeedSeconds / ANIMATION_LENGTH_SECONDS
- sound.Volume = Util.Clamp(sound.Volume + normalizedIncrement * stepScale, 0, 1)
- else
- sound.Volume = 0
- end
- else
- Util.Pause(sound)
- end
- end
- local sound = Sounds[SFX.Running]
- if activeState == Enum.HumanoidStateType.Running and Util.HorizontalSpeed(Head) < 0.5 then
- Util.Pause(sound)
- end
- end
- local lastTick = tick()
- local TICK_SPEED_SECONDS = 0.25
- while true do
- onUpdate(tick() - lastTick, TICK_SPEED_SECONDS)
- lastTick = tick()
- wait(TICK_SPEED_SECONDS)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement