Hello fellow devforum users! I am making a game that involves a system where players tag each other. However, I have a problem. While making the tagging system, I tried to implement a debounce to make sure that players can’t tag each other every split second. That’s where the problem comes in.
Let’s say that player X and player Y ran into each other, with one being tagged and the other not.
If I make a script that only checks the debounce within the player it is responsible for, maybe player X for example, there is a chance that player Y with a debounce of false
may interact with player X with a debounce of true
, causing only player Y to switch tag values, breaking the system. However, if I make a script that checks the debounce of both players that interact with each other, if player X and Y both have a false
debounce, X’s script may change the value faster than Y’s script, causing Y’s script to assume that X’s debounce was true
and not change player Y’s tag value.
How would I fix this issue?
Also thanks for reading the entire thing if you did, I know it is a lot to take in and can get confusing.
u can add a global debounce system and keep track of everyone who got tagged and and then use it for verifying ur tagging, lets say Player Y is tagged so
Tag(PlayerY)
GlobalDebounce[PlayerY] = true
task.wait(2)
GlobalDebounce[PlayerY] = false
now if PlayerY is try to unexpectedly counter tag PlayerX, we can check if PlayerY was tagged before PlayerX
if GlobalDebounce[PlayerY] then
return
end
if u have any questions or if i properly didnt answer ur question, let me know
ok just made a script that follows that is this what you mean
local taggedEvent = game:GetService("ServerStorage").ServerEvents.PlayerTagged -- Event that fires when a player is tagged
local Players = game:GetService("Players")
local playerCheck = {} -- Stores the players that the script is checking, to prevent the script from tagging the same player multiple times
local debounceDictionary = { -- Stores the debounces of the players
}
local function checkDebounce(player)
local plrId = player.UserId
return debounceDictionary[plrId]
end
local function onPlayerAdded(player)
local plrId = player.UserId
debounceDictionary[plrId] = false
end
taggedEvent.Event:Connect(function(player, enemyplayer)
if table.find(playerCheck, player) and table.find(playerCheck, enemyplayer) then
return
else
table.insert(playerCheck, player)
table.insert(playerCheck, enemyplayer)
local plrDebounce = checkDebounce(player)
local enemyDebounce = checkDebounce(player)
if plrDebounce == false and enemyDebounce == false then
debounceDictionary[player.UserId] = true
player.Tagged.Value = not player.Tagged.Value -- Player's tagged value stored inside the player
debounceDictionary[enemyplayer.UserId] = true
enemyplayer.Tagged.Value = not enemyplayer.Tagged.Value
task.wait(1)
debounceDictionary[player.UserId] = false
debounceDictionary[enemyplayer.UserId] = false
end
for idx, plr in playerCheck do
if plr == player or plr == enemyplayer then
table.remove(playerCheck, idx)
end
end
end
end)
Players.PlayerAdded:Connect(onPlayerAdded)