Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why isn't my touched table working properly?

Asked by
Sorukan 240 Moderation Voter
6 years ago

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.

01local checkList = {}
02local debounce2 = false
03 
04hitBox.Touched:Connect(function(hit)
05    if hit.Parent:FindFirstChild('Humanoid') and not debounce2 then
06        debounce2 = 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            debounce2 = false
16            end
17        end
18    end
19end)
0
u cant do that u just have to make multiple touched events i think tacotown2 119 — 6y
0
?? Sorukan 240 — 6y
0
for do nothing just have it say return and for tables i would recommend using strings and the name of the object like hit.Parent.Name TiredMelon 405 — 6y
0
it didn't make a difference sorry Sorukan 240 — 6y

1 answer

Log in to vote
1
Answered by
brianush1 235 Moderation Voter
6 years ago

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:

01local checkList = {}
02 
03hitBox.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
12end)
Ad

Answer this question