Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isn't my touched table working properly?

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)

4 comments

0

u cant do that u just have to make multiple touched events i think

0

??

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

0

it didn't make a difference sorry

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.