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.

local checkList = {}
local debounce2 = false

hitBox.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') and not debounce2 then
        debounce2 = true

        for i,v in pairs(checkList) do
            if v == hit.Parent then
            -- do nothing
        elseif v ~= hit.Parent then
        table.insert(checkList,hit.Parent)
            v.Humanoid:TakeDamage(10)
            wait(1)
            debounce2 = false
            end
        end
    end
end)
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:

local checkList = {}

hitBox.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        if not checkList[hit.Parent] then -- If the player is not in the table:
            v.Humanoid:TakeDamage(10) -- Take damage
            checkList[hit.Parent] = true -- Store true with the key hit.Parent
            wait(1)
            checkList[hit.Parent] = nil -- And then remove it so that they can once again take damage
        end
    end
end)
Ad

Answer this question