How to find out which part got touched

Hey i’m DevFoll and i used a for i, v in pairs() loop for touched event but i need to find out which part got touched NOT BODY PART JUST THE part the player touched

for i, v in pairs(game.Workspace.Checkpoints:GetChildren()) do
	v.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local ColorChangingScript = game.ReplicatedStorage.ColorChangingScript:Clone()
			ColorChangingScript.Parent = Player.PlayerGui
			ColorChangingScript.Disabled = false
			game.ReplicatedStorage.PartThatGotTouched.Value = --idk?
			game.ReplicatedStorage.ChangeColor:FireClient(Player)
		end
	end)
end

and when i thought i found out i struggled with somthing else idk how to refrence it when definding where the part is lemme show you

game.ReplicatedStorage.ChangeColor.OnClientEvent:Connect(function()
	local PartThatGotTouched = game.ReplicatedStorage.PartThatGotTouched.Value
	game.Workspace.Checkpoints["PartThatGotTouched"].Color = Color3.fromRGB(136, 255, 8)
end)

Is your PartThatGotTouched an object value or a string value? Also does all of the Checkpoints children have a different name?

If the PartThatGotTouched is a StringValue and all of the Checkpoints children have different names then try this

Try this

for i, v in pairs(game.Workspace.Checkpoints:GetChildren()) do
	v.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local ColorChangingScript = game.ReplicatedStorage.ColorChangingScript:Clone()
			ColorChangingScript.Parent = Player.PlayerGui
			ColorChangingScript.Disabled = false
			game.ReplicatedStorage.PartThatGotTouched.Value = v.Name
			game.ReplicatedStorage.ChangeColor:FireClient(Player)
		end
	end)
end
game.ReplicatedStorage.ChangeColor.OnClientEvent:Connect(function()
	local PartThatGotTouched = game.ReplicatedStorage.PartThatGotTouched.Value
	game.Workspace.Checkpoints[PartThatGotTouched].Color = Color3.fromRGB(136, 255, 8)
end)

Yes PartThatGotTouched is a stringValue and yes all of the checkpoints have diffrent names.

OMG, It works THANK YOU SO MUCH.

1 Like