Touch Event not working

Hello Developers
So i’m having troubles with this touch event. as you can see in the video, i am touching the part with a saber, but when it touched, it does nothing. I assume it’s on the properties but i already tried that but i failed. I tried searching but it doesn’t really help any because it’s about the health event/ coin event/ etc. so as you can see in the script, it should unanchor the parent and play a sound. but it didn’t play any or did unanchor the object.


Script:

script.Parent.Hit.Touched:Connect(function(saber)
		script.Parent.Slash:Play()
		script.Parent.Anchored = false
end)

Part 1 Properties: (Enemy Part)

Part 2 Properties: (Saber)

2 Likes

Have you confirmed whether or not it’s firing using print statements?

1 Like

yes i tried that, that’s actually the first version of that script

script.Parent.Hit.Touched:Connect(function(saber)
	print('blue')
	if saber.Name == 'SaberBlue' then
		script.Parent.Slash:Play()
		script.Parent.Anchored = false
	end
end)

Does this script works then? Did the print statement gets fired?

1 Like

unfortunately, it did not print any.

If the script isn’t parented to the block you should use you should use a for loop:

for _, v in pairs(script.Parent:GetChildren()) do
	
	v.Touched:Connect( function(touch)
		
		-- stuff
	end)
end

I think it’s not working because you’re only connecting the touch function to only one block, so only this one will work.

I need to leave so i can’t answer anymore, sorry.

1 Like

it didn’t work but thanks for helping!

You should show people your explorer, and the code probably didn’t worked because other things may be parented to script’s parent, so you need to check if v == a hit block.

image
this is the explorer. The part named “Slice” was formerly named “hit” btw

Hello everyone!
i fixed it now, i instead used WorldRoot:ArePartsTouchingOthers.

script.Parent.Touched:Connect(function(saber)
	print('started')
end)
script.Parent.TouchEnded:Connect(function(saber)
	print('ended')
end)
while wait(0.1) do
	if game.Workspace:ArePartsTouchingOthers({game.Workspace.SaberBlue,script.Parent},0) then
		script.Parent.Parent.Anchored = false
		script.Parent.audio:Play()
	end
end

and you may ask, why did i still placed the Touched/TouchEnded even though the solution was game.Workspace:ArePartsTouchingOthers. Well i tried removing it but it completely make it not working so i leave it there.Thank you too! to people who helped me, i really appreciate it!

Just so you’re aware, touch events might not be reliable enough for this project, especially on harder/faster modes requiring fast movement.

If you get stuck, I suggest using Raycasts or even something like Raycast Hitbox as touch events can sometimes miss activation.

2 Likes