yoitzErrorYT

rewind time Lua roblox

Jun 20th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. local key = "E" --key to intiate the flashback. see https://create.roblox.com/docs/reference/engine/enums/KeyCode for an exhaustive list
  2. local flashbacklength = 60 --how long the flashback should be stored in approx seconds
  3. local flashbackspeed = 1 --how many frames to skip during flashback (set to 0 to disable)
  4.  
  5. local name = game:GetService("RbxAnalyticsService"):GetSessionId() --unique id that games cannot access but does not change on subsequent executions (used for the name of the binded function)
  6. local frames,uis,LP,RS = {},game:GetService("UserInputService"),game:GetService("Players").LocalPlayer,game:GetService("RunService") --set some vars
  7.  
  8. pcall(RS.UnbindFromRenderStep,RS,name) --unbind the function if previously binded
  9.  
  10. local function getchar()
  11.    return LP.Character or LP.CharacterAdded:Wait()
  12. end
  13.  
  14. function gethrp(c) --gethrp ripped from my env script and stripped of arguments
  15. return c:FindFirstChild("HumanoidRootPart") or c.RootPart or c.PrimaryPart or c:FindFirstChild("Torso") or c:FindFirstChild("UpperTorso") or c:FindFirstChildWhichIsA("BasePart")
  16. end
  17.  
  18. local flashback = {lastinput=false,canrevert=true}
  19.  
  20. function flashback:Advance(char,hrp,hum,allowinput)
  21.    
  22.    if #frames>flashbacklength*60 then --make sure we don't have too much history
  23.        table.remove(frames,1)
  24.    end
  25.    
  26.    if allowinput and not self.canrevert then
  27.        self.canrevert = true
  28.    end
  29.    
  30.    if self.lastinput then --make sure platformstand goes back to normal
  31.        hum.PlatformStand = false
  32.        self.lastinput = false
  33.    end
  34.    
  35.    table.insert(frames,{
  36.        hrp.CFrame,
  37.        hrp.Velocity,
  38.        hum:GetState(),
  39.        hum.PlatformStand,
  40.        char:FindFirstChildOfClass("Tool")
  41.    })
  42. end
  43.  
  44. function flashback:Revert(char,hrp,hum)
  45.    local num = #frames
  46.    if num==0 or not self.canrevert then --add to history and return if no history is present
  47.        self.canrevert = false
  48.        self:Advance(char,hrp,hum)
  49.        return
  50.    end
  51.    for i=1,flashbackspeed do --skip frames (if enabled)
  52.        table.remove(frames,num)
  53.        num=num-1
  54.    end
  55.    self.lastinput = true
  56.    local lastframe = frames[num]
  57.    table.remove(frames,num)
  58.    hrp.CFrame = lastframe[1]
  59.    hrp.Velocity = -lastframe[2]
  60.    hum:ChangeState(lastframe[3])
  61.    hum.PlatformStand = lastframe[4] --platformstand to make flying look normal again
  62.    local currenttool = char:FindFirstChildOfClass("Tool")
  63.    if lastframe[5] then --equip/unequip tools
  64.        if not currenttool then
  65.            hum:EquipTool(lastframe[5])
  66.        end
  67.    else
  68.        hum:UnequipTools()
  69.    end
  70. end
  71.  
  72. local function step() --function that runs every frame
  73.    local char = getchar()
  74.    local hrp = gethrp(char)
  75.    local hum = char:FindFirstChildWhichIsA("Humanoid")
  76.    if uis:IsKeyDown(Enum.KeyCode[key]) then --begin flashback
  77.        flashback:Revert(char,hrp,hum)
  78.    else
  79.        flashback:Advance(char,hrp,hum,true)
  80.    end
  81. end
  82. RS:BindToRenderStep(name,1,step) --finally, bind our function
Tags: lua
Add Comment
Please, Sign In to add comment