Is there a better way to do touched events?

Hi, I have a bit of script that changes the colour of a part while a player is touching it. However I found that, say the players leg is the thing to touch the part first, if the leg stops touching the part, even if the players arm is still touching it; the parts colour changes back to normal. Any ideas how I could make this less buggy?

T1P1.Touched:Connect(function(touchedPart) -- TOUCHED PART IS THE PART OF THE CHARACTER THAT TOUCHES THE PART, SO BY GETTING ITS PARENT, I'M GETTING THE PLAYERS CHARACTER
	local Player = Players:GetPlayerFromCharacter(touchedPart.Parent) -- FUNCTION TO GET THE PLAYER FROM CHARACTER
	
	if T1Player1 == nil then
		T1Player1 = Player
		T1Player1Prev = Player
		T1B1.BrickColor = BrickColor.new("Teal")
		T1B1.Material = Enum.Material.Neon
		T1P1.Transparency = 0.75
		T1P2.Base.Transparency = 0
		checkReady()
	end
end)

T1P1.TouchEnded:Connect(function(touchedPart)
	local Player = Players:GetPlayerFromCharacter(touchedPart.Parent)
	
	-- Checks that it was player 1 that stopped touching the part since this function would fire if another player came along and touched the part and then stopped touching it
	if T1Player1 == Player then
		T1Player1Prev = T1Player1
		T1Player2Prev = T1Player2
		T1Player3Prev = T1Player3
		T1Player1 = nil
		T1Player2 = nil
		T1Player3 = nil
		T1B1.BrickColor = BrickColor.new("Smoky grey")
		T1B2.BrickColor = BrickColor.new("Smoky grey")
		T1B3.BrickColor = BrickColor.new("Smoky grey")
		T1B1.Material = Enum.Material.Plastic
		T1B2.Material =  Enum.Material.Plastic
		T1B3.Material =  Enum.Material.Plastic
		T1P1.Transparency = 1
		T1P2.Base.Transparency = 1
		T1P2.Transparency = 1
		T1P3.Base.Transparency = 1
		T1P3.Transparency = 1
		checkReady()
	end
end)

check if the part that touched the part is the humanoidrootpart that way it will only detect 1 part

1 Like

Its working so much better now, thank you very much :slight_smile:

1 Like

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