The script is a child of a part which should, when it got touched create a billboard gui and text label over a player’s head. But ofc the player touches the Block multiple times while walking trough it. So i was planning to create a table, where each time when someone touches it the billboard gui and label get created and the player name gets added to the table.
If the player touches it another time, the script should reckognize him from the table.
How do i do that?
I’ve already searched trough the half forum but not really found anything
Instead of using a local variable, you use dictionary keys. You need to find if there’s a player object associated first and if so then trigger the debounce. Here’s some rough code you can use as a template for understanding how to do this, but it’s not a drop-in resolution to your problem so use it as a learning template more than me giving you code to use right away.
local Players = game:GetService("Players")
local DEBOUNCE_TIME = 3
local userDebounces = {}
local function touched(hit) -- Assume hit is a limb and not a nested part
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end -- Do nothing, no player
if userDebounces[player.UserId] then return end
task.delay(DEBOUNCE_TIME, function() userDebounces[player.UserId] = false end)
userDebounces[player.UserId] = true
-- Do your thing here
end
local Part = script.Parent
local PlayerDebounces = {}
Part.Touched:Connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
if table.find(PlayerDebounces, Player) then
return
end
table.insert(PlayerDebounces, Player)
--do code here
task.wait(5)
table.remove(PlayerDebounces, table.find(PlayerDebounces, Player))
end
end)
Okay, the concept you are looking for is array/dictionary debouncing. This concept appears complex at first, but once you understand it, it is not.
Here is an example of using a Dict to debounce and the pros and cons of the method.
local Players = game.Players;
local Part = game.Workspace.Part;
local Tick = tick -- faster to store in mem than to call
local DictDeb = {};
Part.Touched:Connect(function(Hit)
local Player = Players:GetPlayerFromCharacter(Hit.Parent);
if Player then
if not ArrayDeb[Player.Name] then
ArrayDeb[Player.Name] = Tick() + 5; -- 5 seconds
if ArrayDeb[Player.Name] and ArrayDeb[Player.Name] > Tick() then
ArrayDeb[Player.Name] = nil; -- reset
end
end
end)
The example above uses a Hash Table or a Dict in Lua; this method is arguably the fastest way to debounce. The reasoning behind that lies in the data structure. Arrays push index over once you insert an object, whereas dictionaries save a key to memory. There is a notation called big O notation; this notation keeps track of the efficiency of code. Most Dict functions are O(1) which is a constant. Conversely, Array functions are majority O(n) which is linear. Linear and continuous-time are massively different in their time comparisons; wherefore, Dictionaries are the best programming choice for debouncing. However, if you want to use arrays, I will include an example below.
local Part = script.Parent
local PlayerDebounces = {}
Part.Touched:Connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
if table.find(PlayerDebounces, Player) then
return
end
table.insert(PlayerDebounces, Player)
--do code here
task.wait(5)
table.remove(PlayerDebounces, table.find(PlayerDebounces, Player))
end
end)