I have this ModuleScript that adds a dictionary to a main table and I use this to determine if a person is stunned or currently doing an action. However, I need to check if that specific dictionary changed so it could cancel that action midway through. I’ve tried firing a BindableEvent when the whole table changes, but checking the whole table if I only need to check a certain player’s dictionary seemed inefficient. I looked in the forum and saw something along the lines of using metatables, but I don’t know how to incorporate it into each dictionary that is added.
The script:
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local status = {}
local statuslist = {}
local Clear = function(array,Status,Player)
for i = #array, 1, -1 do
if array[i][Status] ~= nil and array[i][Status] <= 0 then
table.remove(array,i)
end
end
end
status.AddPlayer = function(Target)
statuslist[Target.Name] = {}
end
status.RemovePlayer = function(Target)
statuslist[Target.Name] = nil
end
status.Clear = function(Target)
statuslist[Target.Name] = {}
end
status.Add = function(Target,Status,Value,Type)
if statuslist[Target.Name] == nil then return end
statuslist[Target.Name][#statuslist[Target.Name]+1] = {[Status] = Value}
SendTable(Target,statuslist[Target.Name])
local index = statuslist[Target.Name][#statuslist[Target.Name]]
if type(Value) == "number" and Type ~= nil and Type == "Timer" then
local connection;
connection = RunService.Heartbeat:Connect(function(dt)
if index[Status] == nil then
Clear(statuslist[Target.Name],Status,Target)
connection:Disconnect()
elseif index[Status] <= 0 then
Clear(statuslist[Target.Name],Status,Target)
connection:Disconnect()
else
index[Status] = index[Status] - dt
end
end)
end
end
status.Remove = function(Status, Target)
if type(Target) ~= "string" then
Target = Target.Name
end
if statuslist[Target] == nil then return end
for i = #statuslist[Target], 1, -1 do
if statuslist[Target][i][Status] ~= nil then
table.remove(statuslist[Target],i)
return
end
end
end
status.Find = function(value,Target)
if type(Target) ~= "string" then
Target = Target.Name
end
local list = status.List()
local array = list[Target]
local found = false
for i = #array, 1, -1 do
if array[i][value] ~= nil then
found = array[i][value]
return found
end
end
return found
end
return status
What could I incorporate so I can find if a specific Player’s dictionary on the statuslist gets changed on another script?
Edit: If you see any bad practices outside of the main topic, feel free to say that too.