How do i make it print when player touched one of certain part without making another script for every part

Hello, i wanna make it print “hello” when player touches one of the certain parts. But i cant do that. Is there a way to do it?

(There is 5-4 parts named “Yehu”)

Here is code:

script.Parent.Yehu.Touched:Connect(function()
print(“hmm idk”)
end)

Make a folder and put all the parts in there
Example:

local Parts = workspace.FOLDERNAME:GetChildren()

Parts.Touched:Connect(function(hit)
      print("Hello")
end

Something like that

1 Like

I think this should work:

local wsdes = workspace:GetDescendants()
for i,v in pairs(wsdes) do
   if v.Name = "Yehu" then
      local yehu_part = v
      yehu_part.Touched:Connect(function()
         print("Hello")
      end)
   end
end
2 Likes

Add a check to see if it was a player that touched the part, and use one of the answers above to easily add the .Touched event to each part (I’d reccomend putting them all the Yehu parts into a single folder/model to avoid needlessly iterating over the entire workspace)

Object.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then -- check if it was a player that touched the part
		print("Hello")
	end
end)
1 Like

Uh it gives this error (aye is model’s name)

image

Have you considered using collection services to avoid repeating script instances?

1 Like