Touched events are not firing correctly

Hello, I am trying to make a trap with some animations that are triggered by a touched event.
As I was making my trap I noticed that sometimes the Touched Event would not work. The Output would just give me “HumanoidRootPart is not a valid member of Accessory…”

Screenshot 2020-12-17 064245

I have already tried fixing this by checking if the character model found is even a model. That did not work.

Here is the code used:

local TweenService = game:GetService("TweenService")

local Robux = script.Parent.Robux

Busy = false

--[[ Main Function ]]--
function start(hit)
	if not Busy then
		Busy = true
		local Char = hit.Parent
		if not Char:IsA("Model") then
			Char = hit.Parent.Parent
		end
		if not Char:IsA("Model") then
			Char = hit.Parent.Parent.Parent
		end
		if not Char:IsA("Model") then
			Char = hit.Parent.Parent.Parent.Parent
		end
		hit.Parent.HumanoidRootPart.CFrame = script.Parent.TPDestination.CFrame
		Char.Humanoid.WalkSpeed = 0
		Char.Humanoid.JumpPower = 0
		wait(5)
		TweenService:Create(script.Parent.Trap1Door1.Hinge1, TweenInfo.new(1, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {CFrame = script.Parent.Trap1Door1.Hinge1Finish.CFrame}):Play()
		script.Parent.Trap1Door1.DoorOpen:Play()
		wait(3)
		TweenService:Create(script.Parent.Trap1Door1.Hinge1, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In), {CFrame = script.Parent.Trap1Door1.Hinge1Start.CFrame}):Play()
		wait(1)
		script.Parent.Trap1Door1.DoorClose:Play()
		script.Parent.Pipe.SoundPart.Vacuum_Start:Play()
		wait(1.044)
		script.Parent.Pipe.SoundPart.Vacuum_Running:Play()
		while Char.Humanoid.Health > 0 do
			wait(0.6)
			local RobuxClone = Robux.Robux:Clone()
			RobuxClone.Parent = script.Parent.RobuxStorage
			TweenService:Create(RobuxClone, TweenInfo.new(3, Enum.EasingStyle.Linear), {CFrame = Robux.Robux1Finish.CFrame}):Play()
			Char.Humanoid:TakeDamage(5)
		end
	end
end


				--[[ TP Walls ]]--
script.Parent.TPWall1.Touched:Connect(function(hit)
	start(hit)
end)

script.Parent.TPWall2:Connect(function(hit)
	start(hit)
end)

script.Parent.TPWall3:Connect(function(hit)
	start(hit)
end)

script.Parent.TPWall4:Connect(function(hit)
	start(hit)
end)

script.Parent.TPWall5:Connect(function(hit)
	start(hit)
end)


I hope you can help me because I have had this problem for a long time now. (in other projects)

1 Like

I dont know the best approach to use Touch event, I just do this to get the function to trigger an event only once, when a HumanoidRootPart is found.

script.Parent.Touched:connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
		local hitPlayer = Players:GetPlayerFromCharacter(hit.Parent)
		print(hit.Name)
		print(hitPlayer)
	end
end)

The error you are experiencing means that you are trying to get a HumanoidRootPart of that Accesory, cause the Accessory touched the Part and triggered the touch event, all parts in the character are triggering the Touch event, I just use that code to isolate the HRP, and fire the touch event only once

1 Like