Really quick blog post here tonight on how to detect collisions between Humanoids in Roblox. My script is named DetectCollisionsBetweenHumanoids and I’ve placed this script under ServerScriptService. Please note, that I have some code in my example which displays text on the users screen whenever a collision is detected, you can ignore this part of the code which displays that a collision has occurred on the screen:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- DisplayTextModule is some code I use to display text on screen for testing, if you see this in a code snippet, ignore this part | |
local DisplayTextModule = require(script.Parent.Parent.StarterGui.ScreenGui.Frame.TextLabel.DisplayTextModuleScript) | |
local Players = game:GetService("Players") | |
-- Works for R15, not sure if this works for R6 or not | |
local function partIsHumanoid(part) | |
if part.Name == "Head" then | |
return true | |
elseif part.Name == "UpperTorso" then | |
return true | |
elseif part.Name == "LowerTorso" then | |
return true | |
elseif part.Name == "LeftFoot" then | |
return true | |
elseif part.Name == "LeftLowerLeg" then | |
return true | |
elseif part.Name == "LeftUpperLeg" then | |
return true | |
elseif part.Name == "RightFoot" then | |
return true | |
elseif part.Name == "RightLowerLeg" then | |
return true | |
elseif part.Name == "RightUpperLeg" then | |
return true | |
elseif part.Name == "LeftHand" then | |
return true | |
elseif part.Name == "LeftLowerArm" then | |
return true | |
elseif part.Name == "LeftUpperArm" then | |
return true | |
elseif part.Name == "RightHand" then | |
return true | |
elseif part.Name == "RightLowerArm" then | |
return true | |
elseif part.Name == "RightUpperArm" then | |
return true | |
else | |
return false | |
end | |
end | |
local function onPlayerAdded(player) | |
local character = player.Character or player.CharacterAdded:Wait() | |
local humanoid = character:WaitForChild("Humanoid") | |
humanoid.Touched:Connect(function(otherPart) | |
local timestampStr = tostring(os.time(os.date("!*t"))) | |
if partIsHumanoid(otherPart) then | |
DisplayTextModule.Display("Collision w/ R15: " .. otherPart.Name) | |
end | |
end) | |
end | |
Players.PlayerAdded:Connect(onPlayerAdded) |