PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Touched

This event fires when one of the Humanoid|Humanoid's limbs come in contact with another BasePart.

The BasePart the Humanoid|Humanoid's limb is touching along with the limb itself is given.

This event will not fire when limbs belonging to the Humanoid come into contact with themselves.

Alternatives to the Humanoid Touched event

Although the Humanoid.Touched event is useful, developers should consider if there are alternatives that suit their needs better before using it.

  • In most cases it is advised to connect a BasePart/Touched event for BasePart|BaseParts of interest instead. This is because the Humanoid Touched event will constantly fire when the humanoid is moving. For example, in a dodgeball game it would be more practical to connect a touched event for the balls rather than the humanoid
  • For developers trying to work out when the Humanoid has landed on the ground, the Humanoid/StateChanged event is more suitable. Alternatively, developers can use Humanoid/FloorMaterial to see if the Humanoid is standing on anything

Notes

  • Connecting to this event will cause a TouchTransmitter to be created in every limb
  • The is currently no equivalent of BasePart/TouchEnded|BasePart.TouchEnded for Humanoid|Humanoids

Parameters

Name Type Default Description

The BasePart the Humanoid has come in contact with

The limb of the Humanoid that has been touched


Code Samples


Midas Touch

When placed inside a Player/Character model this code will give a player the ‘Midas touch’. Everything their character touches will change to gold.

When the Humanoid dies, this change is undone and the golden BasePart|BaseParts are returned to their original state.

To test this out, place this code inside a Script and place it in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.

local character = script.Parent 

local humanoid = character:WaitForChild("Humanoid")

local partInfo = {}

local debounce = false
local touchedConnection = humanoid.Touched:Connect(function(hit, limb)
	if debounce then
		return
	end

	-- make sure this part is valid 
	if not hit.CanCollide or hit.Transparency ~= 0 then
		return
	end

	-- make sure this part hasn't already been made gold
	if not partInfo[hit] then
		-- save info about this part for later
		partInfo[hit] = {
			BrickColor = hit.BrickColor,
			Material = hit.Material
		}

		-- make this part gold
		hit.BrickColor = BrickColor.new("Gold")
		hit.Material = Enum.Material.Ice

		-- apply debounce
		debounce = true
		wait(0.2)
		debounce = false
	end
end)

humanoid.Died:Connect(function()
	-- disconnect touched connection
	if touchedConnection then
		touchedConnection:Disconnect()
	end

	-- undo all of the gold
	for part, info in pairs(partInfo) do
		if part and part.Parent then
			part.BrickColor = info.BrickColor
			part.Material = info.Material
		end
	end
end)