Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. --[[
  2. Author: @spotco
  3. This script runs locally for the player of the given humanoid.
  4. This script triggers humanoid sound play/pause actions locally.
  5.  
  6. The Playing/TimePosition properties of Sound objects bypass FilteringEnabled, so this triggers the sound
  7. immediately for the player and is replicated to all other players.
  8.  
  9. This script is optimized to reduce network traffic through minimizing the amount of property replication.
  10. ]]--
  11.  
  12. --All sounds are referenced by this ID
  13. local SFX = {
  14. Died = 0;
  15. Running = 1;
  16. Swimming = 2;
  17. Climbing = 3,
  18. Jumping = 4;
  19. GettingUp = 5;
  20. FreeFalling = 6;
  21. FallingDown = 7;
  22. Landing = 8;
  23. Splash = 9;
  24. }
  25.  
  26. local Humanoid = nil
  27. local Head = nil
  28. --SFX ID to Sound object
  29. local Sounds = {}
  30. local SoundService = game:GetService("SoundService")
  31.  
  32. do
  33. local Figure = script.Parent.Parent
  34. Head = Figure:WaitForChild("Head")
  35. while not Humanoid do
  36. for _,NewHumanoid in pairs(Figure:GetChildren()) do
  37. if NewHumanoid:IsA("Humanoid") then
  38. Humanoid = NewHumanoid
  39. break
  40. end
  41. end
  42. if Humanoid then break end
  43. Figure.ChildAdded:wait()
  44. end
  45. Sounds[SFX.Died] = Head:WaitForChild("Died")
  46. Sounds[SFX.Running] = Head:WaitForChild("Running")
  47. Sounds[SFX.Swimming] = Head:WaitForChild("Swimming")
  48. Sounds[SFX.Climbing] = Head:WaitForChild("Climbing")
  49. Sounds[SFX.Jumping] = Head:WaitForChild("Jumping")
  50. Sounds[SFX.GettingUp] = Head:WaitForChild("GettingUp")
  51. Sounds[SFX.FreeFalling] = Head:WaitForChild("FreeFalling")
  52. Sounds[SFX.Landing] = Head:WaitForChild("Landing")
  53. Sounds[SFX.Splash] = Head:WaitForChild("Splash")
  54.  
  55. local DefaultServerSoundEvent = game:GetService("ReplicatedStorage"):FindFirstChild("DefaultServerSoundEvent")
  56. if DefaultServerSoundEvent then
  57. DefaultServerSoundEvent.OnClientEvent:connect(function(sound, playing, resetPosition)
  58. if UserSettings():IsUserFeatureEnabled("UserPlayCharacterLoopSoundWhenFE") then
  59. if resetPosition and sound.TimePosition ~= 0 then
  60. sound.TimePosition = 0
  61. end
  62. if sound.IsPlaying ~= playing then
  63. sound.Playing = playing
  64. end
  65. else
  66. if sound.TimePosition ~= 0 then
  67. sound.TimePosition = 0
  68. end
  69. if not sound.IsPlaying then
  70. sound.Playing = true
  71. end
  72. end
  73. end)
  74. end
  75. end
  76.  
  77. local IsSoundFilteringEnabled = function()
  78. return game.Workspace.FilteringEnabled and SoundService.RespectFilteringEnabled
  79. end
  80.  
  81. local Util
  82. Util = {
  83.  
  84. --Define linear relationship between (pt1x,pt2x) and (pt2x,pt2y). Evaluate this at x.
  85. YForLineGivenXAndTwoPts = function(x,pt1x,pt1y,pt2x,pt2y)
  86. --(y - y1)/(x - x1) = m
  87. local m = (pt1y - pt2y) / (pt1x - pt2x)
  88. --float b = pt1.y - m * pt1.x;
  89. local b = (pt1y - m * pt1x)
  90. return m * x + b
  91. end;
  92.  
  93. --Clamps the value of "val" between the "min" and "max"
  94. Clamp = function(val,min,max)
  95. return math.min(max,math.max(min,val))
  96. end;
  97.  
  98. --Gets the horizontal (x,z) velocity magnitude of the given part
  99. HorizontalSpeed = function(Head)
  100. local hVel = Head.Velocity + Vector3.new(0,-Head.Velocity.Y,0)
  101. return hVel.magnitude
  102. end;
  103.  
  104. --Gets the vertical (y) velocity magnitude of the given part
  105. VerticalSpeed = function(Head)
  106. return math.abs(Head.Velocity.Y)
  107. end;
  108.  
  109. --Setting Playing/TimePosition values directly result in less network traffic than Play/Pause/Resume/Stop
  110. --If these properties are enabled, use them.
  111. Play = function(sound)
  112. if IsSoundFilteringEnabled() then
  113. sound.CharacterSoundEvent:FireServer(true, true)
  114. end
  115. if sound.TimePosition ~= 0 then
  116. sound.TimePosition = 0
  117. end
  118. if not sound.IsPlaying then
  119. sound.Playing = true
  120. end
  121. end;
  122.  
  123. Pause = function(sound)
  124. if UserSettings():IsUserFeatureEnabled("UserPlayCharacterLoopSoundWhenFE") and IsSoundFilteringEnabled() then
  125. sound.CharacterSoundEvent:FireServer(false, false)
  126. end
  127. if sound.IsPlaying then
  128. sound.Playing = false
  129. end
  130. end;
  131.  
  132. Resume = function(sound)
  133. if UserSettings():IsUserFeatureEnabled("UserPlayCharacterLoopSoundWhenFE") and IsSoundFilteringEnabled() then
  134. sound.CharacterSoundEvent:FireServer(true, false)
  135. end
  136. if not sound.IsPlaying then
  137. sound.Playing = true
  138. end
  139. end;
  140.  
  141. Stop = function(sound)
  142. if UserSettings():IsUserFeatureEnabled("UserPlayCharacterLoopSoundWhenFE") and IsSoundFilteringEnabled() then
  143. sound.CharacterSoundEvent:FireServer(false, true)
  144. end
  145. if sound.IsPlaying then
  146. sound.Playing = false
  147. end
  148. if sound.TimePosition ~= 0 then
  149. sound.TimePosition = 0
  150. end
  151. end;
  152. }
  153.  
  154. do
  155. -- List of all active Looped sounds
  156. local playingLoopedSounds = {}
  157.  
  158. -- Last seen Enum.HumanoidStateType
  159. local activeState = nil
  160.  
  161. -- Verify and set that "sound" is in "playingLoopedSounds".
  162. function setSoundInPlayingLoopedSounds(sound)
  163. for i=1, #playingLoopedSounds do
  164. if playingLoopedSounds[i] == sound then
  165. return
  166. end
  167. end
  168. table.insert(playingLoopedSounds,sound)
  169. end
  170.  
  171. -- Stop all active looped sounds except parameter "except". If "except" is not passed, all looped sounds will be stopped.
  172. function stopPlayingLoopedSoundsExcept(except)
  173. for i=#playingLoopedSounds,1,-1 do
  174. if playingLoopedSounds[i] ~= except then
  175. Util.Pause(playingLoopedSounds[i])
  176. table.remove(playingLoopedSounds,i)
  177. end
  178. end
  179. end
  180.  
  181. -- Table of Enum.HumanoidStateType to handling function
  182. local stateUpdateHandler = {
  183. [Enum.HumanoidStateType.Dead] = function()
  184. stopPlayingLoopedSoundsExcept()
  185. local sound = Sounds[SFX.Died]
  186. Util.Play(sound)
  187. end;
  188.  
  189. [Enum.HumanoidStateType.RunningNoPhysics] = function()
  190. stateUpdated(Enum.HumanoidStateType.Running)
  191. end;
  192.  
  193. [Enum.HumanoidStateType.Running] = function()
  194. local sound = Sounds[SFX.Running]
  195. stopPlayingLoopedSoundsExcept(sound)
  196.  
  197. if Util.HorizontalSpeed(Head) > 0.5 then
  198. Util.Resume(sound)
  199. setSoundInPlayingLoopedSounds(sound)
  200. else
  201. stopPlayingLoopedSoundsExcept()
  202. end
  203. end;
  204.  
  205. [Enum.HumanoidStateType.Swimming] = function()
  206. if activeState ~= Enum.HumanoidStateType.Swimming and Util.VerticalSpeed(Head) > 0.1 then
  207. local splashSound = Sounds[SFX.Splash]
  208. splashSound.Volume = Util.Clamp(
  209. Util.YForLineGivenXAndTwoPts(
  210. Util.VerticalSpeed(Head),
  211. 100, 0.28,
  212. 350, 1),
  213. 0,1)
  214. Util.Play(splashSound)
  215. end
  216.  
  217. do
  218. local sound = Sounds[SFX.Swimming]
  219. stopPlayingLoopedSoundsExcept(sound)
  220. Util.Resume(sound)
  221. setSoundInPlayingLoopedSounds(sound)
  222. end
  223. end;
  224.  
  225. [Enum.HumanoidStateType.Climbing] = function()
  226. local sound = Sounds[SFX.Climbing]
  227. if Util.VerticalSpeed(Head) > 0.1 then
  228. Util.Resume(sound)
  229. stopPlayingLoopedSoundsExcept(sound)
  230. else
  231. stopPlayingLoopedSoundsExcept()
  232. end
  233. setSoundInPlayingLoopedSounds(sound)
  234. end;
  235.  
  236. [Enum.HumanoidStateType.Jumping] = function()
  237. if activeState == Enum.HumanoidStateType.Jumping then
  238. return
  239. end
  240. stopPlayingLoopedSoundsExcept()
  241. local sound = Sounds[SFX.Jumping]
  242. Util.Play(sound)
  243. end;
  244.  
  245. [Enum.HumanoidStateType.GettingUp] = function()
  246. stopPlayingLoopedSoundsExcept()
  247. local sound = Sounds[SFX.GettingUp]
  248. Util.Play(sound)
  249. end;
  250.  
  251. [Enum.HumanoidStateType.Freefall] = function()
  252. if activeState == Enum.HumanoidStateType.Freefall then
  253. return
  254. end
  255. local sound = Sounds[SFX.FreeFalling]
  256. sound.Volume = 0
  257. stopPlayingLoopedSoundsExcept()
  258. end;
  259.  
  260. [Enum.HumanoidStateType.FallingDown] = function()
  261. stopPlayingLoopedSoundsExcept()
  262. end;
  263.  
  264. [Enum.HumanoidStateType.Landed] = function()
  265. stopPlayingLoopedSoundsExcept()
  266. if Util.VerticalSpeed(Head) > 75 then
  267. local landingSound = Sounds[SFX.Landing]
  268. landingSound.Volume = Util.Clamp(
  269. Util.YForLineGivenXAndTwoPts(
  270. Util.VerticalSpeed(Head),
  271. 50, 0,
  272. 100, 1),
  273. 0,1)
  274. Util.Play(landingSound)
  275. end
  276. end;
  277.  
  278. [Enum.HumanoidStateType.Seated] = function()
  279. stopPlayingLoopedSoundsExcept()
  280. end;
  281. }
  282.  
  283.  
  284.  
  285. -- Handle state event fired or OnChange fired
  286. function stateUpdated(state)
  287. if stateUpdateHandler[state] ~= nil then
  288. stateUpdateHandler[state]()
  289. end
  290. activeState = state
  291. end
  292.  
  293.  
  294.  
  295. Humanoid.Died:connect( function() stateUpdated(Enum.HumanoidStateType.Dead) end)
  296. Humanoid.Running:connect( function() stateUpdated(Enum.HumanoidStateType.Running) end)
  297. Humanoid.Swimming:connect( function() stateUpdated(Enum.HumanoidStateType.Swimming) end)
  298. Humanoid.Climbing:connect( function() stateUpdated(Enum.HumanoidStateType.Climbing) end)
  299. Humanoid.Jumping:connect( function() stateUpdated(Enum.HumanoidStateType.Jumping) end)
  300. Humanoid.GettingUp:connect( function() stateUpdated(Enum.HumanoidStateType.GettingUp) end)
  301. Humanoid.FreeFalling:connect( function() stateUpdated(Enum.HumanoidStateType.Freefall) end)
  302. Humanoid.FallingDown:connect( function() stateUpdated(Enum.HumanoidStateType.FallingDown) end)
  303.  
  304.  
  305.  
  306. -- required for proper handling of Landed event
  307.  
  308. Humanoid.StateChanged:connect(function(old, new)
  309. stateUpdated(new)
  310. end)
  311.  
  312.  
  313.  
  314. function onUpdate(stepDeltaSeconds, tickSpeedSeconds)
  315. local stepScale = stepDeltaSeconds / tickSpeedSeconds
  316. do
  317. local sound = Sounds[SFX.FreeFalling]
  318. if activeState == Enum.HumanoidStateType.Freefall then
  319. if Head.Velocity.Y < 0 and Util.VerticalSpeed(Head) > 75 then
  320. Util.Resume(sound)
  321.  
  322. --Volume takes 1.1 seconds to go from volume 0 to 1
  323. local ANIMATION_LENGTH_SECONDS = 1.1
  324.  
  325. local normalizedIncrement = tickSpeedSeconds / ANIMATION_LENGTH_SECONDS
  326. sound.Volume = Util.Clamp(sound.Volume + normalizedIncrement * stepScale, 0, 1)
  327. else
  328. sound.Volume = 0
  329. end
  330. else
  331. Util.Pause(sound)
  332. end
  333. end
  334.  
  335. do
  336. local sound = Sounds[SFX.Running]
  337. if activeState == Enum.HumanoidStateType.Running then
  338. if Util.HorizontalSpeed(Head) < 0.5 then
  339. Util.Pause(sound)
  340. end
  341. end
  342. end
  343. end
  344.  
  345.  
  346. local lastTick = tick()
  347. local TICK_SPEED_SECONDS = 0.25
  348. while true do
  349. onUpdate(tick() - lastTick,TICK_SPEED_SECONDS)
  350. lastTick = tick()
  351. wait(TICK_SPEED_SECONDS)
  352. end
  353.  
  354. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement