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

How do I check if parts in table were touched?

Asked by
Nickelz 37
5 years ago
Edited 5 years ago

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
0
No Rare_tendo 3000 — 5y
0
No... U Nickelz 37 — 5y
0
No me Rare_tendo 3000 — 5y
0
Yes no u Nickelz 37 — 5y
View all comments (6 more)
0
Actually, no u Rare_tendo 3000 — 5y
0
no no me, no u Nickelz 37 — 5y
0
No me, no u Rare_tendo 3000 — 5y
0
Try using recursion? Psvita65594 -5 — 5y
0
Whats recursion..? Nickelz 37 — 5y
0
calling a function inside itself. its usually completely unnecessary but hey http://wiki-origin.roblox.com/index.php?title=Recursion Gey4Jesus69 2705 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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.

Ad

Answer this question