Finding part being touched in function

I got help making a script (client-side) that detects when a player hits an object inside of a folder. After the player touches said object, the object that the player touched should be invisible for a couple of seconds. (all of this would be client-side)
The issue is that there’s no way to locate the part that was touched by the player. The script is a local script so it cant be in the parts the player can touch. I need a way to get the script to locate which part was touched. I have found some similar problems that I don’t know how to apply to my situation.

Example: (The glowing part should go invisible for a couple of seconds when you touch it)

local MainFol = workspace:WaitForChild("EssenceTrainingModel"
local canGet = true

local function onTouch(otherPart) --otherpart in my case is in the player, not the part the player is touching.
	local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if player and canGet then
			canGet = false
			player.PlayerGui.Quest.ScoreBarFrame.TrainingAmount.AmountValue.Value = player.PlayerGui.Quest.ScoreBarFrame.TrainingAmount.AmountValue.Value + 1
			print("got value!")
			touchedpart.ParticleEmitter.Enabled = false -- "Touchedpart" is the part in the folder that the player has touched, but I havent located what part that is yet.
			print("part gone")
			task.wait(2)
			touchedpart.ParticleEmitter.Enabled = true
			print("part return")
			end
			canGet = true
end

for _, parts in pairs(MainFol:GetChildren()) do
	if parts:IsA("BasePart") then
		parts.Touched:Connect(onTouch)
	end
end

I did ask something similar to this before but I need to make a variation of it.

Instead of call the onTouch function from a function created for each part.

function onTouch(part, otherPart)
	-- onTouch code here
end

for _, parts in pairs(MainFol:GetChildren()) do
	if parts:IsA("BasePart") then
		parts.Touched:Connect(function(otherPart)
			onTouch(parts, otherPart)
		end)
	end
end
2 Likes

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