Untitled

 avatar
unknown
plain_text
10 months ago
1.8 kB
15
Indexable
```lua
local Server = require(script.Parent.Parent);

return function (Player)
	local Data      = {
		Proxy = {};
		Tags = {};
	};
	
	local Index     	  = 0;
	local CurrentThread   = nil;
	local LastCall 		  = 0;

	local function Clear(Table)
		table.clear(Table)
		Index = 0;
		LastCall = 0;
		CurrentThread = nil
		if Player then Server.Namespace.packets.CombatTag.sendTo(0, Player) end;
	end

	local Meta = {
		__newindex = function(_, __, Value)
			if Value then
				if Value.Add then
					local Time = workspace:GetServerTimeNow();

					if Data.Tags[Value.Add] then
						Data.Tags[Value.Add].Time   = Time
						Data.Tags[Value.Add].Amount += Value.Value;

					else
						Index += 1;
						Data.Tags[Value.Add] = {Amount = Value.Value, Time = Time}
					end

					local Duration = 60;
					if Value.Duration then Duration = Value.Duration end;

					--// Only overwrite if the duration is bigger than the time remaining
					if (Time - LastCall) < Duration or LastCall == 0 then
						if Player then Server.Namespace.packets.CombatTag.sendTo(Time+Duration, Player) end;

						if CurrentThread then task.cancel(CurrentThread); CurrentThread = nil end;
						CurrentThread = task.delay(Duration, Clear, Data.Tags);
					end

					LastCall = Time;
					
				elseif Value.Remove then
					if Data.Tags[Value.Remove] then 
						Data.Tags[Value.Remove] = nil 
						Index -= 1;

						if Index <= 0 then 
							Clear(Data.Tags)
						end
					end;
				end
			end
		end,

		__call = function(_, __)
			if CurrentThread then task.cancel(CurrentThread); CurrentThread = nil end;
			Clear(Data.Tags); -- Flush
		end,

		__len = function(_)
			return Index
		end,
	};

	setmetatable(Data.Proxy, Meta);
	return Data;
end
```
Editor is loading...
Leave a Comment