Currently creating a StateHandler that essentially adds and removes “tags” associated with a player. For instance, when stunned, the StateHandler will add a “tag” to a dictionary under the player’s name.
i.e
[Player] = {
Client = {
Stunned = 1
}
Server = {}
}
Keep in mind that you can be stunned multiple times, therefore it’s important that I can easily add/remove the number (frequency) associated with every tag.
However, I should also be able to add a tag for a certain amount of time before it gets automatically removed. This is where my problem is though, for some reason I can’t create multiple threads inside of the function, therefore it’s impossible for me to remove multiple tags that have a set duration using task.spawn/coroutine.wrap.
function StateHandler.AddClientState(EntityName, StateName, Duration)
if not StateHandler.ActiveEntries.Client[EntityName] then
warn("Could not find active client state entry for " .. EntityName)
return
end
if not StateHandler.ActiveEntries.Client[EntityName][StateName] then
StateHandler.ActiveEntries.Client[EntityName][StateName] = 1
if Duration then
task.spawn(function()
print("Waiting")
task.wait(Duration)
print("Removing")
StateHandler.RemoveClientState(EntityName, StateName)
end)
end
else
StateHandler.ActiveEntries.Client[EntityName][StateName] += 1
end
end
function StateHandler.RemoveClientState(EntityName, StateName)
if not StateHandler.ActiveEntries.Client[EntityName] then
warn("Could not find active client state entry for " .. EntityName)
return
end
if StateHandler.ActiveEntries.Client[EntityName][StateName] then
StateHandler.ActiveEntries.Client[EntityName][StateName] -= 1
if StateHandler.ActiveEntries.Client[EntityName][StateName] <= 0 then
StateHandler.ActiveEntries.Client[EntityName][StateName] = nil
end
end
end
Output:
In this case, I added the tag (Client1) twice to the dictionary for a duration of 3 seconds, the first time works as expected, however the second time I add the client state (line 3), it doesn’t create a new thread. (It doesn’t print out “Waiting” like it did the first time). Then once I display the entries, I see that it only removed it the first time, and never again after that.
I’d really appreciate any help with this, thanks.