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:
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!
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
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.
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.
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.
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!