I would suggest a game logic strategy for updating all queuing humanoids every so often. That way there's no need to worry about disconnecting and reconnecting events at an unnecessarily high rate, or breaking loops. Here's my personal tested solution to your problem...
local part = workspace:WaitForChild("Baseplate")
local tableRemove = table.remove
local function removeFromQueue(queue, key)
for i = 1, #queue do
if queue[i] == key then
tableRemove(queue, i)
end
end
end
local function isInQueue(queue, key)
for i = 1, #queue do
if queue[i] == key then
return true
end
end
end
local function addToQueue(queue, entry)
queue[#queue + 1] = entry
end
local damageQueue = setmetatable({}, {
__index = {
add = function(self, human, hit)
local entry = self:get(human)
if entry and not isInQueue(entry[1], hit) then
addToQueue(entry[1], hit)
elseif not entry then
addToQueue(self, {{hit}, human})
end
end,
get = function(self, human)
for i = 1, #self do
if self[i][2] == human then
return self[i]
end
end
end,
remove = function(self, human, hit)
local entry = self:get(human)
if entry then
removeFromQueue(entry[1], hit)
if #entry[1] == 0 then
for i = 1, #self do
if self[i][2] == human then
tableRemove(self, i)
end
end
end
end
end,
step = function(self, damage)
for i = 1, #self do
local human = self[i][2]
if human.Health > 0 then
self[i][2]:TakeDamage(damage)
end
end
end
}
})
part.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChildOfClass("Humanoid")
if human then
damageQueue:add(human, hit)
end
end)
part.TouchEnded:Connect(function(hit)
local human = hit.Parent:FindFirstChildOfClass("Humanoid")
if human then
damageQueue:remove(human, hit)
end
end)
while wait(0.1) do
damageQueue:step(1)
end
If you have any questions, just let me know.
1 comment
basically just add a touchended event