Touch event not working on client [SOLVED]

Hello. I am making a script that detects when a part is touched from a client script and for some reason it doesn’t appear to work.

I checked the code to make sure there weren’t no issues and there wasn’t. Then I tried switching the script to server and all of a sudden it started working. When I switched it back to client it stopped working again.

Here’s the code incase your interested:
image

No clue why this is happening, I tried doing research to see if anyone else had a issue like this and I haven’t come across anything. Any help is appreciated!

2 Likes

Local scripts only run when it is a descendant of specific objects such as StarterPlayerScripts, ReplicatedFirst, StarterGui, etc. It wouldn’t work when parented to a part.

You can do this instead:

local parts = game.Workspace.PartsFolder -- change this to whatever path you need to get the parts

for _, v in parts:GetChildren() do
     v.Touched:Connect(function(hit)
          print(hit)
     end)
end

Make sure to parent the this to StartGui or some other container that allows local scripts to run

2 Likes

Wait, can you use remote events though?

2 Likes

This isn’t a local script. I’m using a script but I changed the runcontext to client. Sorry I should’ve mentioned that.

1 Like

I can but not sure if it’s necessary for what I’m trying to do…

1 Like

Also something else is that I did try before putting a local script in StarterGui and doing the same thing and it still didn’t work. So regardless it still wouldn’t solve my problem.

1 Like

Why are you using for i, v in pairs for the touched event for?

Ive never used runcontext but my method should work. Is there any errors that pops up when you try it?

I’m using it because there’s multiple parts in the folder that are gonna share the same function. So I jus connect the function to every part.

1 Like

There isn’t any errors. I’ll try it out but I doubt it would work because as I said, even when I tried using a local script in StarterGui, it still wasn’t working.

The method you gave me didn’t work.

It should work as localscripts do run under StarterGui. Are there any errors and did you change the pathing?

1 Like

Yes I did change the pathing and no there aren’t any errors. I’m really unsure why this isn’t working.

I just tried it and it does work. Could you give some more context to what is happening? Is the folder(or whatever container your using) of parts being created after the game runs?

Ok I just tried putting this function into a new local script as before I was putting it into a different one and now it’s working. Only thing I’m confused about is why it wasn’t working when I had the script with the runcontext being client.

1 Like

Im not sure how runcontext works so maybe someone else could answer that. However, from my quick search, I dont think runcontext is meant to be used in this way

I’ve used it in this way before so I don’t know. I’ll do research. I know why it didn’t work in the local script before. The problem is pretty much solved but thank you for your guys help, really appreciate it!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.