I'm trying to make a part that'll damage humanoids that touch it but only if they are not in the table. Once they've been touched, they'll be put inside the table and won't be able to take any more damage for one second.
01 | local checkList = { } |
02 | local debounce 2 = false |
03 |
04 | hitBox.Touched:Connect( function (hit) |
05 | if hit.Parent:FindFirstChild( 'Humanoid' ) and not debounce 2 then |
06 | debounce 2 = true |
07 |
08 | for i,v in pairs (checkList) do |
09 | if v = = hit.Parent then |
10 | -- do nothing |
11 | elseif v ~ = hit.Parent then |
12 | table.insert(checkList,hit.Parent) |
13 | v.Humanoid:TakeDamage( 10 ) |
14 | wait( 1 ) |
15 | debounce 2 = false |
16 | end |
17 | end |
18 | end |
19 | end ) |
Table keys are very useful for this. Instead of looping through the table, you could change the way you store information such that accessing it is easier for your purposes:
01 | local checkList = { } |
02 |
03 | hitBox.Touched:Connect( function (hit) |
04 | if hit.Parent:FindFirstChild( 'Humanoid' ) then |
05 | if not checkList [ hit.Parent ] then -- If the player is not in the table: |
06 | v.Humanoid:TakeDamage( 10 ) -- Take damage |
07 | checkList [ hit.Parent ] = true -- Store true with the key hit.Parent |
08 | wait( 1 ) |
09 | checkList [ hit.Parent ] = nil -- And then remove it so that they can once again take damage |
10 | end |
11 | end |
12 | end ) |