Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. -- handles Animating OTHER rigs, other than our local Luanoid.
  2.  
  3. local References = require(game:GetService("ReplicatedStorage").Utilities.Services.References)
  4. local Utilities = References.Utilities
  5. local Services = Utilities.Services
  6.  
  7. local Api = require(References.PlayerScripts.Main:WaitForChild("Character"):WaitForChild("Controller"):WaitForChild("Api"))
  8.  
  9. local Other = {}
  10. -- list of rigs with stored animation tracks.
  11. Other.Rigs = {}
  12.  
  13. function Other:SetupRig(rig)
  14.  
  15. if not self.Rigs[rig] then -- anti-leak
  16. self.Rigs[rig] = {}
  17. self.Rigs[rig].Connection = rig.AncestryChanged:Connect(function()
  18. if not rig.Parent then
  19. self.Rigs[rig].Connection:Destroy()
  20. self.Rigs[rig] = nil
  21. end
  22. end)
  23. end
  24.  
  25. self.Rigs[rig].Animations = self.Rigs[rig].Animations or {}
  26. end
  27.  
  28. function Other:MakeTrack(controller, id)
  29. if controller and id then
  30. local animation = Instance.new("Animation")
  31. animation.AnimationId = id
  32. return controller:LoadAnimation(animation)
  33. end
  34. end
  35.  
  36. function Other:Animate(rig, name, id, play_stop)
  37. local controller = rig and rig:FindFirstChild("AnimationController") or Instance.new("AnimationController")
  38. controller.Parent = rig
  39. if controller then
  40.  
  41. self:SetupRig(rig)
  42. local track = self.Rigs[rig].Animations[name]
  43. if track then
  44. local track_id = track.Animation.AnimationId
  45. if track_id ~= id then
  46. -- make new track. A different anim id for the given name is being play (alt/weighted anim handling)
  47. track = self:MakeTrack(controller, id)
  48. self.Rigs[rig].Animations[name] = track
  49. end
  50. if play_stop then
  51. track:Play()
  52. else
  53. track:Stop()
  54. end
  55. else
  56. if play_stop then
  57. track = self:MakeTrack(controller, id)
  58. self.Rigs[rig].Animations[name] = track
  59. track:Play()
  60. --else
  61. -- -- being told to stop an animation we don't have a track for, who cares.
  62. end
  63. end
  64. end
  65. end
  66.  
  67. function Other:AdjustSpeed(rig, name, id, speed)
  68. -- get or make track
  69. -- track:AdjustSpeed(speed)
  70. local controller = rig and rig:FindFirstChild("AnimationController") or Instance.new("AnimationController")
  71. controller.Parent = rig
  72.  
  73. self:SetupRig(rig)
  74.  
  75. local track = self.Rigs[rig].Animations[name] or self:MakeTrack(controller, id)
  76. self.Rigs[rig].Animations[name] = track
  77.  
  78. track:AdjustSpeed(speed)
  79. end
  80.  
  81. Api.Bindables.ReplicateAnimation.Event:Connect(function(rig, name, id, play_stop)
  82. -- get or load the track into rig.AnimationController
  83. Other:Animate(rig, name, id, play_stop)
  84. end)
  85.  
  86.  
  87.  
  88. return Other
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement