Advertisement
toonrun123

LoadCharacterRoblox.

Mar 1st, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.64 KB | None | 0 0
  1. --Thank SirHurt For Remote.
  2. local SFX = {
  3.     Died = 0,
  4.     Running = 1,
  5.     Swimming = 2,
  6.     Climbing = 3,
  7.     Jumping = 4,
  8.     GettingUp = 5,
  9.     FreeFalling = 6,
  10.     FallingDown = 7,
  11.     Landing = 8,
  12.     Splash = 9
  13. }
  14. local Humanoid, Head
  15. local Sounds = {}
  16. local SoundService = game:GetService("SoundService")
  17. local soundEventFolderName = "DefaultSoundEvents"
  18. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  19. local AddCharacterLoadedEvent, RemoveCharacterEvent
  20. local soundEventFolder = ReplicatedStorage:FindFirstChild(soundEventFolderName)
  21. if not soundEventFolder then
  22.     soundEventFolder = Instance.new("Folder", ReplicatedStorage)
  23.     soundEventFolder.Name = soundEventFolderName
  24.     soundEventFolder.Archivable = false
  25. end
  26. RemoveCharacterEvent = soundEventFolder:FindFirstChild("RemoveCharacterEvent")
  27. if RemoveCharacterEvent == nil then
  28.     RemoveCharacterEvent = Instance.new("RemoteEvent", soundEventFolder)
  29.     RemoveCharacterEvent.Name = "RemoveCharacterEvent"
  30. end
  31. AddCharacterLoadedEvent = soundEventFolder:FindFirstChild("AddCharacterLoadedEvent")
  32. if AddCharacterLoadedEvent == nil then
  33.     AddCharacterLoadedEvent = Instance.new("RemoteEvent", soundEventFolder)
  34.     AddCharacterLoadedEvent.Name = "AddCharacterLoadedEvent"
  35. end
  36. AddCharacterLoadedEvent:FireServer()
  37. game.Players.LocalPlayer.CharacterRemoving:connect(function(character)
  38.     RemoveCharacterEvent:FireServer(game.Players.LocalPlayer)
  39. end)
  40. do
  41.     local Figure = script.Parent.Parent
  42.     Head = Figure:WaitForChild("Head")
  43.     while not Humanoid do
  44.         for _, NewHumanoid in pairs(Figure:GetChildren()) do
  45.             if NewHumanoid:IsA("Humanoid") then
  46.                 Humanoid = NewHumanoid
  47.                 break
  48.             end
  49.         end
  50.         if Humanoid then
  51.             break
  52.         end
  53.         Figure.ChildAdded:wait()
  54.     end
  55.     Sounds[SFX.Died] = Head:WaitForChild("Died")
  56.     Sounds[SFX.Running] = Head:WaitForChild("Running")
  57.     Sounds[SFX.Swimming] = Head:WaitForChild("Swimming")
  58.     Sounds[SFX.Climbing] = Head:WaitForChild("Climbing")
  59.     Sounds[SFX.Jumping] = Head:WaitForChild("Jumping")
  60.     Sounds[SFX.GettingUp] = Head:WaitForChild("GettingUp")
  61.     Sounds[SFX.FreeFalling] = Head:WaitForChild("FreeFalling")
  62.     Sounds[SFX.Landing] = Head:WaitForChild("Landing")
  63.     Sounds[SFX.Splash] = Head:WaitForChild("Splash")
  64.     local DefaultServerSoundEvent = soundEventFolder:FindFirstChild("DefaultServerSoundEvent")
  65.     if DefaultServerSoundEvent then
  66.         DefaultServerSoundEvent.OnClientEvent:connect(function(sound, playing, resetPosition)
  67.             if resetPosition and sound.TimePosition ~= 0 then
  68.                 sound.TimePosition = 0
  69.             end
  70.             if sound.IsPlaying ~= playing then
  71.                 sound.Playing = playing
  72.             end
  73.         end)
  74.     end
  75. end
  76. local function IsSoundFilteringEnabled()
  77.     return game.Workspace.FilteringEnabled and SoundService.RespectFilteringEnabled
  78. end
  79. local Util
  80. Util = {
  81.     YForLineGivenXAndTwoPts = function(x, pt1x, pt1y, pt2x, pt2y)
  82.         local m = (pt1y - pt2y) / (pt1x - pt2x)
  83.         local b = pt1y - m * pt1x
  84.         return m * x + b
  85.     end,
  86.     Clamp = function(val, min, max)
  87.         return math.min(max, math.max(min, val))
  88.     end,
  89.     HorizontalSpeed = function(Head)
  90.         local hVel = Head.Velocity + Vector3.new(0, -Head.Velocity.Y, 0)
  91.         return hVel.magnitude
  92.     end,
  93.     VerticalSpeed = function(Head)
  94.         return math.abs(Head.Velocity.Y)
  95.     end,
  96.     Play = function(sound)
  97.         if IsSoundFilteringEnabled() then
  98.             sound.CharacterSoundEvent:FireServer(true, true)
  99.         end
  100.         if sound.TimePosition ~= 0 then
  101.             sound.TimePosition = 0
  102.         end
  103.         if not sound.IsPlaying then
  104.             sound.Playing = true
  105.         end
  106.     end,
  107.     Pause = function(sound)
  108.         if IsSoundFilteringEnabled() then
  109.             sound.CharacterSoundEvent:FireServer(false, false)
  110.         end
  111.         if sound.IsPlaying then
  112.             sound.Playing = false
  113.         end
  114.     end,
  115.     Resume = function(sound)
  116.         if IsSoundFilteringEnabled() then
  117.             sound.CharacterSoundEvent:FireServer(true, false)
  118.         end
  119.         if not sound.IsPlaying then
  120.             sound.Playing = true
  121.         end
  122.     end,
  123.     Stop = function(sound)
  124.         if IsSoundFilteringEnabled() then
  125.             sound.CharacterSoundEvent:FireServer(false, true)
  126.         end
  127.         if sound.IsPlaying then
  128.             sound.Playing = false
  129.         end
  130.         if sound.TimePosition ~= 0 then
  131.             sound.TimePosition = 0
  132.         end
  133.     end
  134. }
  135. do
  136.     local playingLoopedSounds = {}
  137.     local activeState
  138.     local fallSpeed = 0
  139.     function setSoundInPlayingLoopedSounds(sound)
  140.         for i = 1, #playingLoopedSounds do
  141.             if playingLoopedSounds[i] == sound then
  142.                 return
  143.             end
  144.         end
  145.         table.insert(playingLoopedSounds, sound)
  146.     end
  147.     function stopPlayingLoopedSoundsExcept(except)
  148.         for i = #playingLoopedSounds, 1, -1 do
  149.             if playingLoopedSounds[i] ~= except then
  150.                 Util.Pause(playingLoopedSounds[i])
  151.                 table.remove(playingLoopedSounds, i)
  152.             end
  153.         end
  154.     end
  155.     local stateUpdateHandler = {
  156.         [Enum.HumanoidStateType.Dead] = function()
  157.             stopPlayingLoopedSoundsExcept()
  158.             local sound = Sounds[SFX.Died]
  159.             Util.Play(sound)
  160.         end,
  161.         [Enum.HumanoidStateType.RunningNoPhysics] = function(speed)
  162.             stateUpdated(Enum.HumanoidStateType.Running, speed)
  163.         end,
  164.         [Enum.HumanoidStateType.Running] = function(speed)
  165.             local sound = Sounds[SFX.Running]
  166.             stopPlayingLoopedSoundsExcept(sound)
  167.             if activeState == Enum.HumanoidStateType.Freefall and fallSpeed > 0.1 then
  168.                 local vol = math.min(1, math.max(0, (fallSpeed - 50) / 110))
  169.                 local freeFallSound = Sounds[SFX.FreeFalling]
  170.                 freeFallSound.Volume = vol
  171.                 Util.Play(freeFallSound)
  172.                 fallSpeed = 0
  173.             end
  174.             if speed ~= nil and speed > 0.5 then
  175.                 Util.Resume(sound)
  176.                 setSoundInPlayingLoopedSounds(sound)
  177.             elseif speed ~= nil then
  178.                 stopPlayingLoopedSoundsExcept()
  179.             end
  180.         end,
  181.         [Enum.HumanoidStateType.Swimming] = function(speed)
  182.             local threshold = speed
  183.             if activeState ~= Enum.HumanoidStateType.Swimming and threshold > 0.1 then
  184.                 local splashSound = Sounds[SFX.Splash]
  185.                 splashSound.Volume = Util.Clamp(Util.YForLineGivenXAndTwoPts(Util.VerticalSpeed(Head), 100, 0.28, 350, 1), 0, 1)
  186.                 Util.Play(splashSound)
  187.             end
  188.             local sound = Sounds[SFX.Swimming]
  189.             stopPlayingLoopedSoundsExcept(sound)
  190.             Util.Resume(sound)
  191.             setSoundInPlayingLoopedSounds(sound)
  192.         end,
  193.         [Enum.HumanoidStateType.Climbing] = function(speed)
  194.             local sound = Sounds[SFX.Climbing]
  195.             if speed ~= nil and math.abs(speed) > 0.1 then
  196.                 Util.Resume(sound)
  197.                 stopPlayingLoopedSoundsExcept(sound)
  198.             else
  199.                 Util.Pause(sound)
  200.                 stopPlayingLoopedSoundsExcept(sound)
  201.             end
  202.             setSoundInPlayingLoopedSounds(sound)
  203.         end,
  204.         [Enum.HumanoidStateType.Jumping] = function()
  205.             if activeState == Enum.HumanoidStateType.Jumping then
  206.                 return
  207.             end
  208.             stopPlayingLoopedSoundsExcept()
  209.             local sound = Sounds[SFX.Jumping]
  210.             Util.Play(sound)
  211.         end,
  212.         [Enum.HumanoidStateType.GettingUp] = function()
  213.             stopPlayingLoopedSoundsExcept()
  214.             local sound = Sounds[SFX.GettingUp]
  215.             Util.Play(sound)
  216.         end,
  217.         [Enum.HumanoidStateType.Freefall] = function()
  218.             if activeState == Enum.HumanoidStateType.Freefall then
  219.                 return
  220.             end
  221.             local sound = Sounds[SFX.FreeFalling]
  222.             sound.Volume = 0
  223.             stopPlayingLoopedSoundsExcept()
  224.             fallSpeed = math.max(fallSpeed, math.abs(Head.Velocity.y))
  225.         end,
  226.         [Enum.HumanoidStateType.FallingDown] = function()
  227.             stopPlayingLoopedSoundsExcept()
  228.         end,
  229.         [Enum.HumanoidStateType.Landed] = function()
  230.             stopPlayingLoopedSoundsExcept()
  231.             if Util.VerticalSpeed(Head) > 75 then
  232.                 local landingSound = Sounds[SFX.Landing]
  233.                 landingSound.Volume = Util.Clamp(Util.YForLineGivenXAndTwoPts(Util.VerticalSpeed(Head), 50, 0, 100, 1), 0, 1)
  234.                 Util.Play(landingSound)
  235.             end
  236.         end,
  237.         [Enum.HumanoidStateType.Seated] = function()
  238.             stopPlayingLoopedSoundsExcept()
  239.         end
  240.     }
  241.     function stateUpdated(state, speed)
  242.         if stateUpdateHandler[state] ~= nil then
  243.             if state == Enum.HumanoidStateType.Running or state == Enum.HumanoidStateType.Climbing or state == Enum.HumanoidStateType.Swimming or state == Enum.HumanoidStateType.RunningNoPhysics then
  244.                 stateUpdateHandler[state](speed)
  245.             else
  246.                 stateUpdateHandler[state]()
  247.             end
  248.         end
  249.         activeState = state
  250.     end
  251.     Humanoid.Died:connect(function()
  252.         stateUpdated(Enum.HumanoidStateType.Dead)
  253.     end)
  254.     Humanoid.Running:connect(function(speed)
  255.         stateUpdated(Enum.HumanoidStateType.Running, speed)
  256.     end)
  257.     Humanoid.Swimming:connect(function(speed)
  258.         stateUpdated(Enum.HumanoidStateType.Swimming, speed)
  259.     end)
  260.     Humanoid.Climbing:connect(function(speed)
  261.         stateUpdated(Enum.HumanoidStateType.Climbing, speed)
  262.     end)
  263.     Humanoid.Jumping:connect(function()
  264.         stateUpdated(Enum.HumanoidStateType.Jumping)
  265.     end)
  266.     Humanoid.GettingUp:connect(function()
  267.         stateUpdated(Enum.HumanoidStateType.GettingUp)
  268.     end)
  269.     Humanoid.FreeFalling:connect(function()
  270.         stateUpdated(Enum.HumanoidStateType.Freefall)
  271.     end)
  272.     Humanoid.FallingDown:connect(function()
  273.         stateUpdated(Enum.HumanoidStateType.FallingDown)
  274.     end)
  275.     Humanoid.StateChanged:connect(function(old, new)
  276.         stateUpdated(new)
  277.     end)
  278.     function onUpdate(stepDeltaSeconds, tickSpeedSeconds)
  279.         local stepScale = stepDeltaSeconds / tickSpeedSeconds
  280.         do
  281.             local sound = Sounds[SFX.FreeFalling]
  282.             if activeState == Enum.HumanoidStateType.Freefall then
  283.                 if Head.Velocity.Y < 0 and Util.VerticalSpeed(Head) > 75 then
  284.                     Util.Resume(sound)
  285.                     local ANIMATION_LENGTH_SECONDS = 1.1
  286.                     local normalizedIncrement = tickSpeedSeconds / ANIMATION_LENGTH_SECONDS
  287.                     sound.Volume = Util.Clamp(sound.Volume + normalizedIncrement * stepScale, 0, 1)
  288.                 else
  289.                     sound.Volume = 0
  290.                 end
  291.             else
  292.                 Util.Pause(sound)
  293.             end
  294.         end
  295.         local sound = Sounds[SFX.Running]
  296.         if activeState == Enum.HumanoidStateType.Running and Util.HorizontalSpeed(Head) < 0.5 then
  297.             Util.Pause(sound)
  298.         end
  299.     end
  300.     local lastTick = tick()
  301.     local TICK_SPEED_SECONDS = 0.25
  302.     while true do
  303.         onUpdate(tick() - lastTick, TICK_SPEED_SECONDS)
  304.         lastTick = tick()
  305.         wait(TICK_SPEED_SECONDS)
  306.     end
  307. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement