Help with making a rewind ability

Heyo I’m trying to make a rewind ability where the player can enter an amount of time in seconds (ex: 120) and go back and my solution to do this was have a dictionary that holds everything the player does along with the time it happened at that goes up. So if the player picks up an item it will get added to the dictionary with the time of 0 which will go up to 1,2,3,4 etc so it would have been 4 seconds since they have picked it up. hopefully my explanation for my code is decent. Anyways here’s the code looking for some help on how I can improve this the biggest issue atm is the rewindtime function if the time doesn’t exisit I wanna get like the closest thing.

local CurrentTime = os.time()
local Actions = {} --| Will hold every action a player or npc makes
local Debounce = true
local Cooldown = 60

local function Length(Table)
	local counter = 0 
	for _, v in pairs(Table) do
		counter =counter + 1
	end
	return counter
end

--| Gets The Latest Timed Action
--| My idea for this function was to be able to get
--| the latest action and then tell the player like hey you can only rewind this far
local function GetLatestTime(Table)
	local HighestTime = 0
	for i,v in pairs(Table) do
		if v[2] >= HighestTime then
			HighestTime = v[2]
		end
	end
	return HighestTime
end

--| A timer for when the action was made which will go up until 300 seconds (5min) and then get removed (need to do that part still)
local function TimeActionWasMade(Table)
	for i = 0,300 do
		Table[2] = i
		wait(1)
	end
end

Remotes.ActionMade.OnServerEvent:Connect(function(Player,Part)
	table.insert(Actions[Player.Name],{Part,0})
	spawn(function()
		TimeActionWasMade(Actions[Player.Name][Length(Actions[Player.Name])-1])
	end)
end)

Remotes.RewindTime.OnServerEvent:Connect(function(Player,Time)
	if Debounce then
		--Debounce = false
		if Time then
			local AmountToGoBack = tonumber(Time)
			warn("Going back "..AmountToGoBack.." seconds")
			for i,v in ipairs(Actions[Player.Name]["Movement"]) do
				if AmountToGoBack <= tonumber(v[2]) or AmountToGoBack >= tonumber(v[2]) then
					warn("TEST")
					Player.Character.HumanoidRootPart.Position = v[1]
				end
			end
			
			--| Seperate loop for everything else that isnt in movement
			for i,v in pairs(Actions[Player.Name]) do
				if i ~= "Movement" then
					if AmountToGoBack >= tonumber(v[2]) then
						local Test = v[1]:Clone()
						Test.Parent = workspace
					end
				end
			end
		end
		--wait(Cooldown)
		Debounce = true
	else
		warn("On Cooldown")
	end
end)
1 Like