Advertisement
SxScripting

Replay System Local Script

Jun 3rd, 2024
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. --[[
  2. How to Use:
  3.  
  4. Press Record button to record. Hit it again to stop recording.
  5. Enter a number in the Replay Possession box and press enter to replay a recording.
  6.  
  7. Limitation: The tracked instances MUST have unique names.
  8. ]]
  9.  
  10. local Possessions = {}
  11. local Runservice = game:GetService("RunService")
  12.  
  13. -- The Parts that are tracked
  14. local Tracked = {
  15. [1] = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait(),
  16. ["Ball"] = workspace:WaitForChild("Ball"),
  17. }
  18.  
  19. local replaying = false
  20.  
  21. local recording
  22.  
  23. function recordInstanceForTime(i: number)
  24. local table = {}
  25.  
  26. for _, character in pairs(Tracked) do
  27. if character:IsA("Model") then
  28. table[character.Name] = {}
  29. for _, v in pairs(character:GetChildren()) do
  30. if v:IsA("BasePart") then
  31. table[character.Name][v.Name] = v.CFrame
  32. end
  33. end
  34. else
  35. table[character.Name] = character.CFrame
  36. end
  37. end
  38.  
  39. Possessions[#Possessions][i] = table
  40. end
  41.  
  42. function disableJoints(rig: Model)
  43. for _, v in pairs(rig:GetDescendants()) do
  44. if v:IsA("JointInstance") then
  45. if v.Part0.Parent ~= rig or v.Part1.Parent ~= rig then
  46. continue
  47. end
  48.  
  49. v.Enabled = false
  50. elseif v:IsA("BasePart") and v.Parent == rig then
  51. v.Anchored = true
  52. v.CanCollide = false
  53. end
  54. end
  55. end
  56.  
  57. function startReplay(currentPossession: number)
  58. -- Prevent replaying if recording or already replaying
  59. if recording or replaying or currentPossession > #Possessions then
  60. return
  61. end
  62. replaying = true
  63.  
  64. local Models = {}
  65. local tb = Possessions[currentPossession]
  66.  
  67. for _, v: Instance in pairs(Tracked) do
  68. v.Archivable = true
  69.  
  70. local rig = v:Clone()
  71. rig.Parent = workspace
  72. disableJoints(rig)
  73. v.Archivable = false
  74.  
  75. Models[v.Name] = rig
  76. end
  77.  
  78. for _, gotPossession in pairs(tb) do
  79. for character, bodyParts in pairs(gotPossession) do
  80. if typeof(bodyParts) == "CFrame" then
  81. Models[character].CFrame = bodyParts
  82. else
  83. for i, v in pairs(bodyParts) do
  84. Models[character][i].CFrame = v
  85. end
  86. end
  87. end
  88. task.wait()
  89. end
  90.  
  91. for _, v in pairs(Models) do
  92. v:Destroy()
  93. end
  94.  
  95. Models = nil
  96. replaying = false
  97. end
  98.  
  99. local function startRecording()
  100. if replaying then
  101. return
  102. end
  103. if recording then
  104. recording:Disconnect()
  105. recording = nil
  106. script.Parent:WaitForChild("recording").Text = "Recording : false"
  107. return
  108. end
  109. Possessions[#Possessions + 1] = {}
  110. script.Parent:WaitForChild("Possessions").Text = "Amount Of Possessions : " .. #Possessions
  111. local time = 0
  112. recording = Runservice.RenderStepped:Connect(function()
  113. recordInstanceForTime(time)
  114. time += 1
  115. end)
  116.  
  117. script.Parent:WaitForChild("recording").Text = "Recording : true"
  118. end
  119.  
  120. script.Parent:WaitForChild("recording").MouseButton1Click:Connect(function()
  121. startRecording()
  122. end)
  123.  
  124. script.Parent:WaitForChild("TextBox").FocusLost:Connect(function(enterPressed)
  125. if enterPressed then
  126. startReplay(tonumber(script.Parent:WaitForChild("TextBox").Text))
  127. end
  128. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement