Advertisement
ailenkai

Untitled

Jan 6th, 2024
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.76 KB | None | 0 0
  1. --------------------------------------------------------------------------------------
  2. --[[        Set-Up                                                                  ]]
  3. --------------------------------------------------------------------------------------
  4.  
  5. if game:GetService("CoreGui"):FindFirstChild("KeyframeEvents") then
  6.     game:GetService("CoreGui"):FindFirstChild("KeyframeEvents"):Destroy()
  7. end
  8.  
  9. local toolbar = plugin:CreateToolbar("KeyframeEvents")
  10. local button = toolbar:CreateButton("Open GUI", "Keyframe Events for animations", "rbxassetid://10043212933")
  11. button.ClickableWhenViewportHidden = true
  12.  
  13. local Selection = game:GetService("Selection")
  14. local KeyframeSequenceProvider = game:GetService("KeyframeSequenceProvider")
  15. local TweenService = game:GetService("TweenService")
  16. local RunService = game:GetService("RunService")
  17. local StudioService = game:GetService("StudioService")
  18.  
  19. local Folder = script.Parent
  20. local GUI = Folder.KeyframeEvents:Clone()
  21. GUI.Main.Visible = false
  22. local NoModelWarning = GUI.NoModelWarning
  23. local TopFrame = GUI.Main.TopFrame
  24.  
  25. local PartHighlighter = require(Folder.PartHighlighter)
  26. local SliderModule = require(Folder.SliderModule)
  27. local SliderFrame = TopFrame.Info.KeyframesSlider
  28.  
  29. local SelectedKeyframeTextbox = TopFrame.Info.SelectedKeyframe
  30. local dummy = nil
  31. local animator:Animator = nil
  32. local playingAnim = false
  33. local stopAnim = false
  34.  
  35. local ParticleButton = TopFrame.ParticleButton
  36. local ParticleButtonFrame = ParticleButton.Frame
  37. local SoundButton = TopFrame.SoundButton
  38. local SoundButtonFrame = SoundButton.Frame
  39. local PartButton = TopFrame.PartButton
  40. local PartButtonFrame = PartButton.Frame
  41.  
  42. GUI.Parent =  game:GetService("CoreGui")
  43. GUI.Enabled = false
  44.  
  45. local guiVisible = false
  46.  
  47. local function toggleGui()
  48.     guiVisible = not guiVisible
  49.     GUI.Enabled = guiVisible
  50. end
  51.  
  52. local Slider = SliderModule.new(SliderFrame,0.5)
  53.  
  54. --------------------------------------------------------------------------------------
  55. --[[                Functions                                                       ]]
  56. --------------------------------------------------------------------------------------
  57.  
  58. PartHighlighter.Initialize(plugin)
  59. PartHighlighter.ToggleFeature()
  60.  
  61. function weld(x, y)
  62.     if x == y then return end
  63.     local CJ = CFrame.new(x.Position)
  64.     local w = Instance.new("ManualWeld")
  65.     w.Part0 = x
  66.     w.Part1 = y
  67.     w.C0 = x.CFrame:inverse() * CJ
  68.     w.C1 = y.CFrame:inverse() * CJ
  69.     w.Parent = x
  70.     return w
  71. end
  72.  
  73. local t = {}
  74. local rootPart
  75. local track = nil
  76. local function checkSelectionAndShowGui()
  77.     local selectedObjects = Selection:Get()
  78.  
  79.     if #selectedObjects == 1 then
  80.         local selected = selectedObjects[1]
  81.  
  82.         if selected:IsA("Model") and selected:FindFirstChild("AnimSaves") then
  83.             --
  84.             GUI.Main.Visible = true
  85.             NoModelWarning.Visible = false
  86.             stopAnim = false
  87.            
  88.             animator = selected.Humanoid.Animator
  89.            
  90.             t = selected:FindFirstChild("AnimSaves").Potion:GetChildren()
  91.  
  92.             table.sort(t, function(a, b)
  93.                 return a.Time < b.Time  -- Compare the time values
  94.             end)
  95.  
  96.             for i, v in pairs(t) do
  97.                 t[i] = v.Time
  98.             end
  99.             --
  100.            
  101.             local function studioPreviewAnimation(model, animation)
  102.                 -- find the AnimationController and Animator
  103.                 local animationController = model:FindFirstChildOfClass("Humanoid")
  104.                     or model:FindFirstChildOfClass("AnimationController")
  105.                 local animator = animationController and animationController:FindFirstChildOfClass("Animator")
  106.                 if not animationController or not animator then
  107.                     return
  108.                 end
  109.  
  110.                 local asset = KeyframeSequenceProvider:RegisterKeyframeSequence(workspace.KeyframeSequence)
  111.                 local animation = workspace:FindFirstChild("TestAnimation")
  112.                 if animation == nil then
  113.                     animation = Instance.new("Animation")
  114.                     animation.Name = "TestAnimation"
  115.                     animation.AnimationId = asset
  116.                     animation.Parent = workspace
  117.                 end
  118.  
  119.  
  120.                 track = animator:LoadAnimation(animation)
  121.                 --track.Looped = true
  122.                 track:Play()
  123.                 --track:AdjustSpeed(0)
  124.                
  125.                 --[[task.spawn(function()
  126.                     -- preview the animation
  127.                     local startTime = tick()
  128.                     while (tick() - startTime) < track.Length do
  129.                         local step = RunService.Heartbeat:wait()
  130.                         animator:StepAnimations(step)
  131.                         if stopAnim then
  132.                             track:Stop(0)
  133.                             animator:StepAnimations(0)
  134.                             return
  135.                         end
  136.                     end
  137.                 end)]]
  138.                
  139.  
  140.                 -- stop the animation
  141.                 --track:Stop(0)
  142.                 --animator:StepAnimations(0)
  143.  
  144.                 -- reset the joints
  145.                 for _, descendant in pairs(model:GetDescendants()) do
  146.                     if descendant:IsA("Motor6D") then
  147.                         local joint = descendant
  148.                         joint.CurrentAngle = 0
  149.                         joint.Transform = CFrame.new()
  150.                     end
  151.                 end
  152.             end
  153.            
  154.             studioPreviewAnimation(selected)
  155.            
  156.             --[[task.spawn(function()
  157.                 while not stopAnim and task.wait(0.1) do
  158.                     local step = game:GetService("RunService").Heartbeat:wait()
  159.                     animator:StepAnimations(step)
  160.                 end
  161.             end)]]
  162.            
  163.  
  164.            
  165.         else
  166.             NoModelWarning.Visible = true
  167.             GUI.Main.Visible = false
  168.             stopAnim = true
  169.         end
  170.     else
  171.         NoModelWarning.Visible = true
  172.         GUI.Main.Visible = false
  173.     end
  174.    
  175. end
  176.  
  177. local function setAllFramesInvisible()
  178.     ParticleButtonFrame.Visible = false
  179.     SoundButtonFrame.Visible = false
  180.     PartButtonFrame.Visible = false
  181. end
  182.  
  183. local function toggleFrameVisibility(buttonFrame)
  184.     if buttonFrame.Visible then
  185.         setAllFramesInvisible()
  186.     else
  187.         setAllFramesInvisible()
  188.         buttonFrame.Visible = true
  189.     end
  190. end
  191.  
  192. --------------------------------------------------------------------------------------
  193. --[[                            Connections                                         ]]
  194. --------------------------------------------------------------------------------------
  195.  
  196. ParticleButton.MouseButton1Click:Connect(function()
  197.     toggleFrameVisibility(ParticleButtonFrame)
  198. end)
  199.  
  200. SoundButton.MouseButton1Click:Connect(function()
  201.     toggleFrameVisibility(SoundButtonFrame)
  202. end)
  203.  
  204. PartButton.MouseButton1Click:Connect(function()
  205.     toggleFrameVisibility(PartButtonFrame)
  206. end)
  207.  
  208.  
  209. local index = nil
  210. local lastindex = nil
  211. local burger = nil
  212. Slider.Changed:Connect(function(changedValue)
  213.     if #t > 0 then
  214.        
  215.         index = math.round(#t - (#t - (changedValue*#t)))
  216.         if index then
  217.             print("Closest value found at index: " .. index)
  218.             SelectedKeyframeTextbox.Text = "Selected Keyframe: [  ".. index .."  ]"
  219.             --if track and #t > 0 then
  220.             --  track.TimePosition = t[index]
  221.             --end
  222.             local timePerFrame = 0.01
  223.            
  224.             if lastindex then
  225.                 if lastindex > index then
  226.                     timePerFrame = -(index - lastindex)
  227.                 else
  228.                     timePerFrame = index - lastindex
  229.                 end
  230.             end
  231.            
  232.            
  233.             if stopAnim then
  234.                 track:Stop(0)
  235.                 animator:StepAnimations(0)
  236.                 lastindex = index
  237.             end
  238.            
  239.             animator:StepAnimations(timePerFrame)
  240.             lastindex = index
  241.         else
  242.             print("No close value found.")
  243.         end
  244.     end
  245.    
  246. end)
  247.  
  248. Selection.SelectionChanged:Connect(checkSelectionAndShowGui)
  249.  
  250. button.Click:Connect(toggleGui)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement