How to efficiently check if a certain part name is touched by player

So let’s say we have 100 parts with the same name of “TouchBlock”, how do I make it so if the player touches the part with that specific name the script will print “touched” but it will not print if the player touches a part with another name?

(I wouldn’t want to insert a touched script in every part).

2 Likes

Couldn’t you just put a touched event inside of the player’s character instead of all of the parts? And then track the name of the parts the character touches.

1 Like

You could either put all TouchBlock in the workspace or put it in a folder, preferably the latter.

Excuse me for the improper indentation, writing this on my phone:

for _, v in pairs(workspace.TouchBlockFolder:GetChildren()) do
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then -- if it's a character
print("touched")
end
end)
end

However, if you do want to put it in workspace and make a check for the name; you can do this.

for _, v in pairs(workspace:GetChildren()) do
if v.Name == "TouchBlock" and v:IsA("BasePart") then
-- do the same stuff above
end
end
3 Likes