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

How do I check if parts in table were touched?

from what I understand, while loops can be very demanding to a player's gameplay, slowing the game down. So while I was making this I could find no other way of doing this without a while loop. Is there a better way to check if a part in a table has been touched?

My attempt:

local model = game:GetService("Workspace").Model
local table = {}
for i,v in pairs (model:GetChildren()) do -- This part is different, It's for the sake of the site
	table.insert(table,#table+1,v)
end
for i,v in pairs(table) do
	v.Touched:Connect(function()
		print("Touched")
	end)
end

10 comments

0

No

0

No... U

0

No me

0

Yes no u

0

Actually, no u

0

no no me, no u

0

No me, no u

0

Try using recursion?

0

Whats recursion..?

0

calling a function inside itself. its usually completely unnecessary but hey http://wiki-origin.roblox.com/index.php?title=Recursion

1 answer

You can actually use a single table [as called by :GetChildren()] to run this function

local model = workspace:WaitForChild("Model")

local function Fired()
	print("Touched")
end

for _, parts in pairs(model:GetChildren()) do
	parts.Touched:Connect(Fired)
end

If you use a local event, then by using the for loop to check the array, anytime an iterated part is touched, this pre-defined function will fire.

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