For loop creating touched events only works server side?

Here is the code.

for _,v in pairs(WaterWL) do
	
   v.Touched:Connect(function(Ball)
          -- (...)
   end)

end

Why wouldn’t this work on the client?

EDIT: Function is left out on purpose. If a print is inserted in the function, it only works server side.

For starters that code is pretty impractical since the next step in your loop will only run after the touched event is called

Also it would help if you could tell us what that (WaterWL) variable is

Thats the point. ‘WaterWL’ is just a table with all the bricks that should be given a touched event. I left out the function within the actual code because it isn’t necessary.

I think it is going to start by listening for the touched event on the first brick in your table
so basically until it is touched all other bricks are ignored, that might be your problem

Events are handled in a separate thread, it’s not it.

Works perfectly fine serverside. The only reason I am reconsidering doing this on the server is because of latency.

Do you get any errors for running this code, or does this simply not work?

No errors.

Try printing out contents of WaterML, it’s possible that there were no children at the time you were setting variable.

1 Like

A great way to listen for all of your parts to be touched is by tagging them all using CollectionService
and then calling your function when one of the tagged parts is hit, also referencing CollectionService.

2 Likes

I may be wrong, but I believe that CollectionService can be referenced, edited by server and client, which would fit you need for client/server flexibility

please check my facts I may be wrong

Doesn’t print anything, not even nil.

That means that it’s empty.
How are you setting the table?
Also, try printing #WaterML to make sure it’s empty. (Should return 0)

1 Like
local WaterWL = workspace.Water:GetChildren()

“Water” is a folder containing all bricks I want to get a touched event.

I had tried just putting one brick in the folder, waiting for it, and then adding it to a table and it still didn’t work.

( I don’t know of any way to wait for multiple children )

Try using CTRL+F to find occurances of WaterML and whether it was overwritten somewhere later on in the code.

It might seem silly, but did you verify that the local script is actually running? It would also be helpful to see the output of #WaterML like @Legoracer mentioned to verify the local script is actually finding all the parts. I think only at that point could you confirm it is actually the Touched event that isn’t working.

Due to how parts replicate to the client, it is best to use WaitForChild for indexing anything.

I can’t think of any reason behind this. Try adding print() in the for loop AND in the start and end of the function so that we can know if:
a) the script actually goes through any part at all
b) the Touched event actually gets called
c) the function in the Touched event ends correctly

Hello,
Just wondering where you are running the client version from as client scripts in the workspace only run inside of the character model.

2 Likes

Yup! You are correct. Not only that, when using CollectionService you can add a tag on the client and remove it from the server and vice versa. Using CollectionService is definitely a good habit to get into in my opinion, especially for this use case.

My approach to this situation would be:

local WaterWL = {
	workspace:WaitForChild("TestPart", 1),
	workspace:WaitForChild("TestPart2", 1)
}

local CollectionService = game:GetService("CollectionService")

for _,v in pairs (WaterWL) do
	CollectionService:AddTag(v, "TouchingParts")
end

for _,v in pairs (CollectionService:GetTagged("TouchingParts")) do
	v.Touched:Connect(function()
		print("Touching!")
	end)
end
1 Like

Great explanation!