Touch event broken

So I have an NPC that when its LowerTorso touches a part in a particular group in workspace, the NPC will get deleted. I am not sure why, but the touch event seems to just not get triggered.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent == workspace.Road.Hit then
		script.Parent.Parent:Destroy()
	end
end)
3 Likes

Can you show us what script.Parent and script.Parent.Parent are?

1 Like

Switch the localscript to a serverscript.

2 Likes

I think the issue is that your script is a LocalScript, it should be a regular script.

1 Like

I tried that, but it didn’t work.

I tried that, but it didn’t work. Just tried again as well. :confused:

I couldn’t help but notice the part you want the function to activate on is part of a “Road” group. It may be that your LowerTorso is touching the Road, but normally you walk on a road. This isn’t the issue is it?

Well, No. Hits are actually blocks on the road which is tall enough to touch the lower torso.

is “workspace” defined? ie… above the function have you written something like
local workspace = game:GetService("Workspace") ?

also check hit if its exist

script.Parent.Touched:Connect(function(hit)
    local Road = workspace:FindFirstChild("Road")
    local RoadHit = Road:FindFirstChild("Hit")
	if hit and Road and RoadHit and hit.Parent and hit.Parent == Roadhit  then
		script.Parent.Parent:Destroy()
	end
end)
1 Like
script.Parent.Touched:Connect(function(hit)
	if hit == workspace.Road.Hit then
		script.Parent.Parent:Destroy()
	end
end)

Here’s the solution. This code should work.
hit.Parent == workspace.Road.Hit will never evaluate true unless your LowerTorso is touching a child of workspace.Road.Hit.

Unfortunately, it didn’t work :frowning:

Unfortunately, it didn’t work as well :frowning:

Is your script throwing any errors into console?
If it’s not, then your logic simply isn’t correct. Which is what my previous fix attempted to address.

I tested my last code in my own game and it executed fine, as long as my LowerTorso made contact with game.Workspace.Road.Hit

try to check the part name? I dont think this will work

script.Parent.Touched:Connect(function(hit)
    local Road = workspace:FindFirstChild("Road")
    local RoadHit = Road:FindFirstChild("Hit")
	if hit and Road and RoadHit and hit.Parent and hit.Parent.Name == Roadhit.Name  then
		script.Parent.Parent:Destroy()
	end
end)
1 Like

i think this is bug you need to report to roblox.

Well, the part in the Hit Model in welded to one part. Would that make any difference?

yes it’s make a change on the character model.

No, it shouldn’t make a difference.

I would print(hit.Parent) before your if statement to check what it actually is, and make sure you are checking for collision between the correct two objects.