Trigger a local function inside of a local script when a player collides with an object

Basically, I need to trigger a local function inside of a local script when a player touches a part. How would I do this? I understand the basics of scripting so no need to go super in depth. Thanks!

1 Like
--This is my local function
local function DoStuff()
	
end

--Loop through all the parts of the charater
for _,Part in Character:GetChildren() do
	--If the child is a part then
	if Part:IsA("BasePart") then
		--Connect DoStuff
		Part.Touched:Connect(DoStuff)
	end
end
2 Likes

Thanks for the quick reply! I’ll try this out when I get the chance. Thanks!

Edit:
Works great! Marked as Solution.

There’s a specific event/signal you can use for this instead named Humanoid.Touched.
https://developer.roblox.com/en-us/api-reference/event/Humanoid/Touched

local Script = script
local Character = Script.Parent --Server/local script inside 'StarterCharacterScripts'.
local Humanoid = Character:WaitForChild("Humanoid")

local function OnHumanoidTouched(Part, Limb)
	local Output = string.format("Character's %s touched %s.", Limb.Name, Part.Name)
	print(Output)
end

Humanoid.Touched:Connect(OnHumanoidTouched)

image

2 Likes