remi_

TimeToPastModule

Oct 7th, 2020 (edited)
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.69 KB | None | 0 0
  1. --Normal script
  2.  
  3. local tweenService = game:GetService("TweenService")
  4. local runS = game:GetService("RunService")
  5.  
  6. local function makePart(kind, cframe)
  7.     cframe = cframe or CFrame.new()
  8.     local cube = Instance.new("Part")
  9.     local shapes = {["cube"] = Enum.PartType.Block,["sphere"] = Enum.PartType.Ball,["cylinder"] = Enum.PartType.Cylinder}
  10.     kind = shapes[kind] ~= nil and kind or "cube"
  11.     cube.Shape = shapes[kind]
  12.     cube.Name = kind:gsub("^%l", string.upper)
  13.     cube.Size = Vector3.new(4,4,4)
  14.     local faces = {"Back","Front","Bottom","Left","Right","Top"}
  15.     for i = 1,6 do
  16.         local texture = Instance.new("Texture")
  17.         texture.Texture = "http://www.roblox.com/asset/?id=4570698755"
  18.         texture.StudsPerTileU = 2
  19.         texture.StudsPerTileV = 2
  20.         texture.Face = faces[i]
  21.         texture.Parent = cube
  22.     end
  23.     cube.Parent = workspace.PartsFolder
  24.     cube.CFrame = cframe
  25.     return cube
  26. end
  27.  
  28. local function doLoop(object)
  29.     runS.Stepped:Connect(function()-- Waits for the physcis to run.
  30.         if not object.instance then return end-- Checks if the instance of the object exists.
  31.         if object.traveling == false then-- Checks if the object is not traveling to the past, so when is traveling, no positions are saved.
  32.             if #object.positions > 0 then
  33.                 if object.positions[#object.positions] ~= object.instance.CFrame then
  34.                     object.positions[#object.positions+1] = object.instance.CFrame
  35.                 end
  36.             else
  37.                 object.positions[#object.positions+1] = object.instance.CFrame
  38.             end
  39.             if #object.positions > object.maxPos then-- If the amount of positions in the table is greater than the max amount, then the first one gets deleted.
  40.                 table.remove(object.positions, 1)
  41.             end
  42.         end
  43.     end)
  44. end
  45.  
  46. local function travel(object)
  47.     if object.traveling == false and object.instance then
  48.         -- Setting values.
  49.         object.traveling = true
  50.         local oldColor = object.instance.Color
  51.         local oldAnchored = object.instance.Anchored
  52.         object.instance.Color = Color3.fromRGB(96, 108, 149)
  53.         object.instance.CanCollide = false
  54.         object.instance.Anchored = true
  55.         -- Simple code to itherate a numerical table backwards.
  56.         for i = #object.positions, 1, -1 do
  57.             local v = object.positions[i]
  58.             if v then
  59.                 local magnitude
  60.                 magnitude = (object.instance.Position - v.Position).Magnitude
  61.                 if magnitude < 1 then
  62.                     magnitude = 1
  63.                 end
  64.                 local info = TweenInfo.new(0.03 * magnitude, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
  65.                 local tween = tweenService:Create(object.instance, info, {CFrame = v})
  66.                 tween:Play()
  67.                 tween.Completed:Wait()
  68.             end
  69.         end
  70.         object.positions = {}-- Resetting the table.
  71.         object.instance.Color = oldColor
  72.         object.instance.Anchored = oldAnchored
  73.         object.instance.CanCollide = true
  74.         object.traveling = false
  75.     end
  76. end
  77.  
  78. local class = {}
  79. class.__index = class
  80.  
  81. function class.new(instance)
  82.     if getmetatable(self) ~= nil then return warn("Attempt to perform constructor in object.") end
  83.     if not instance then instance = makePart("cube") end
  84.     local object = setmetatable({},class)
  85.     object.instance = instance-- Instance of the object.
  86.     object.positions = { }-- Table where positions are saved.
  87.     object.maxPos = 175-- Amount of positions that can be saved, this means that if you have more positions, it will go further to the past.
  88.     object.traveling = false-- Bool value to check if an object is "travelling" to the past.
  89.     local tE = Instance.new("RemoteFunction")-- This is for test purposes only, if you want it to fire anotehr way, do it.
  90.     tE.Name = "Travel"
  91.     tE.Parent = object.instance
  92.     tE.OnServerInvoke =function(plr, inf)
  93.         if plr.Character and (plr.Character.HumanoidRootPart.Position-object.instance.Position).Magnitude < 25 then
  94.             if inf == "t" then
  95.                 object:Travel()
  96.             elseif inf == "c" then
  97.                 return object.traveling and true or false
  98.             end
  99.         end
  100.     end
  101.     object:Initialize()
  102.     return object
  103. end
  104.  
  105. function class.newEmpty(kind, cframe)
  106.     if getmetatable(self) ~= nil then return warn("Attempt to perform constructor in object.") end
  107.     cframe = cframe or CFrame.new(5,0,5)
  108.     local avaible = {"cube", "sphere"}
  109.     kind = table.find(avaible, kind) and kind or "cube"
  110.     local instance = makePart(kind,cframe)
  111.     class.new(instance)
  112. end
  113.  
  114. function class:Initialize()
  115.     if getmetatable(self) ~= class then return warn("Attempt to perform method in class.") end
  116.     doLoop(self)
  117. end
  118.  
  119. function class:Travel()
  120.     if getmetatable(self) ~= class then return warn("Attempt to perform method in class.") end
  121.     travel(self)
  122. end
  123.  
  124. return class
  125.  
  126. --Test script( used to have a more visual context )
  127.  
  128. local tweenService = game:GetService("TweenService")
  129. local runS = game:GetService("RunService")
  130.  
  131. local function createPoint(cframe)
  132.     local point = Instance.new("Part")
  133.     point.Size = Vector3.new(0.2,0.2,0.2)
  134.     point.Anchored = true
  135.     point.CanCollide = false
  136.     point.Color = Color3.fromRGB(85, 255, 255)
  137.     point.Material = Enum.Material.Neon
  138.     point.CFrame = cframe
  139.     point.Transparency = 0.5
  140.     local mesh = Instance.new("SpecialMesh")
  141.     mesh.MeshType = Enum.MeshType.Sphere
  142.     mesh.Parent = point
  143.     point.Parent = workspace
  144.     return point
  145. end
  146.  
  147. local function makePart(kind, cframe)
  148.     cframe = cframe or CFrame.new()
  149.     local cube = Instance.new("Part")
  150.     local shapes = {["cube"] = Enum.PartType.Block,["sphere"] = Enum.PartType.Ball,["cylinder"] = Enum.PartType.Cylinder}
  151.     kind = shapes[kind] ~= nil and kind or "cube"
  152.     cube.Shape = shapes[kind]
  153.     cube.Name = kind:gsub("^%l", string.upper)
  154.     cube.Size = Vector3.new(4,4,4)
  155.     local faces = {"Back","Front","Bottom","Left","Right","Top"}
  156.     for i = 1,6 do
  157.         local texture = Instance.new("Texture")
  158.         texture.Texture = "http://www.roblox.com/asset/?id=4570698755"
  159.         texture.StudsPerTileU = 2
  160.         texture.StudsPerTileV = 2
  161.         texture.Face = faces[i]
  162.         texture.Parent = cube
  163.     end
  164.     cube.Parent = workspace.PartsFolder
  165.     cube.CFrame = cframe
  166.     return cube
  167. end
  168.  
  169. local function doLoop(object)
  170.     runS.Stepped:Connect(function()-- Waits for the physcis to run.
  171.         if not object.instance then return end-- Checks if the instance of the object exists.
  172.         if object.traveling == false then-- Checks if the object is not traveling to the past, so when is traveling, no positions are saved.
  173.             print(#object.positions)
  174.             if #object.positions > 0 then
  175.                 if object.positions[#object.positions] ~= object.instance.CFrame then
  176.                     object.positions[#object.positions+1] = object.instance.CFrame
  177.                     table.insert(object.points, createPoint(object.instance.CFrame))
  178.                 end
  179.             else
  180.                 object.positions[#object.positions+1] = object.instance.CFrame
  181.                 table.insert(object.points, createPoint(object.instance.CFrame))
  182.             end
  183.             if #object.positions > object.maxPos then-- If the amount of positions in the table is greater than the max amount, then the first one gets deleted.
  184.                 object.points[1]:Destroy()
  185.                 table.remove(object.points, 1)
  186.                 table.remove(object.positions, 1)
  187.             end
  188.         end
  189.     end)
  190. end
  191.  
  192. local function travel(object)
  193.     if object.traveling == false and object.instance then
  194.         -- Setting values.
  195.         object.traveling = true
  196.         local oldColor = object.instance.Color
  197.         local oldAnchored = object.instance.Anchored
  198.         object.instance.Color = Color3.fromRGB(96, 108, 149)
  199.         object.instance.CanCollide = false
  200.         object.instance.Anchored = true
  201.         -- Simple code to itherate a numerical table backwards.
  202.         for i = #object.positions, 1, -1 do
  203.             local v = object.positions[i]
  204.             if v then
  205.                 local magnitude
  206.                 magnitude = (object.instance.Position - v.Position).Magnitude
  207.                 if magnitude < 1 then
  208.                     magnitude = 1
  209.                 end
  210.                 local info = TweenInfo.new(0.03 * magnitude, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
  211.                 local tween = tweenService:Create(object.instance, info, {CFrame = v})
  212.                 tween:Play()
  213.                 object.points[i]:Destroy()
  214.                 table.remove(object.points, i)
  215.                 tween.Completed:Wait()
  216.             end
  217.         end
  218.         object.positions = {}-- Resetting the table.
  219.         object.points = {}
  220.         object.instance.Color = oldColor
  221.         object.instance.Anchored = oldAnchored
  222.         object.instance.CanCollide = true
  223.         object.traveling = false
  224.     end
  225. end
  226.  
  227. local class = {}
  228. class.__index = class
  229.  
  230. function class.new(instance)
  231.     if getmetatable(self) ~= nil then return warn("Attempt to perform constructor in object.") end
  232.     if not instance then instance = makePart("cube") end
  233.     local object = setmetatable({},class)
  234.     object.instance = instance-- Instance of the object.
  235.     object.positions = { }-- Table where positions are saved.
  236.     object.points = { }
  237.     object.maxPos = 175-- Amount of positions that can be saved, this means that if you have more positions, it will go further to the past.
  238.     object.traveling = false-- Bool value to check if an object is "travelling" to the past.
  239.     local tE = Instance.new("RemoteFunction")-- This is for test purposes only, if you want it to fire anotehr way, do it.
  240.     tE.Name = "Travel"
  241.     tE.Parent = object.instance
  242.     tE.OnServerInvoke =function(plr, inf)
  243.         if plr.Character and (plr.Character.HumanoidRootPart.Position-object.instance.Position).Magnitude < 25 then
  244.             if inf == "t" then
  245.                 object:Travel()
  246.             elseif inf == "c" then
  247.                 return object.traveling and true or false
  248.             end
  249.         end
  250.     end
  251.     return object
  252. end
  253.  
  254. function class.newEmpty(kind, cframe)
  255.     if getmetatable(self) ~= nil then return warn("Attempt to perform constructor in object.") end
  256.     cframe = cframe or CFrame.new(5,0,5)
  257.     local avaible = {"cube", "sphere"}
  258.     kind = table.find(avaible, kind) and kind or "cube"
  259.     local instance = makePart(kind,cframe)
  260.     class.new(instance)
  261. end
  262.  
  263. function class:Initialize()
  264.     if getmetatable(self) ~= class then return warn("Attempt to perform method in class.") end
  265.     doLoop(self)
  266. end
  267.  
  268. function class:Travel()
  269.     if getmetatable(self) ~= class then return warn("Attempt to perform method in class.") end
  270.     travel(self)
  271. end
  272.  
  273. return class
  274.  
Add Comment
Please, Sign In to add comment