TouchEnded not working properly

Hi! I’m trying to make a thing that plays an animation and changes the gravity while the player is touching a part, but it fires the TouchEnded event when you move randomly. I’m not really sure how to fix this.


local touchinghums = require(game.ReplicatedStorage.touching)

script.Parent.Touched:Connect(function(hit)
	if hit.ClassName == "Accessory" then
		else
	local Humanoid = hit.Parent:WaitForChild("Humanoid")
	if Humanoid then
		touchinghums[Humanoid] = true

		game.Players:GetPlayerFromCharacter(hit.Parent).falling.Value = true
		local fallanim = script.Parent.fall
		local track = Humanoid:LoadAnimation(fallanim)
		track:Play()
		
		end
		end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local Humanoid = hit.Parent:WaitForChild("Humanoid")
	if Humanoid then
		touchinghums[Humanoid] = nil
		game.Players:GetPlayerFromCharacter(hit.Parent).falling.Value = false
		for i,v in pairs(Humanoid:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		end
end)
2 Likes

Instead of using:

local Humanoid = hit.Parent:WaitForChild("Humanoid")

use “:FindFirstChild” so the code doesnt yield, since waitforchild yields infinite if no timeout parameter is given.

local Humanoid = hit.Parent:FindFirstChild("Humanoid")
1 Like

the actual script works, I just need a way to only detect when the whole character stops touching the part instead of any single part of it.

1 Like

If it’s a small part, try using a large cube the size of a player for testing purposes.

1 Like

It’s huge, there’s no way its a size problem.

1 Like

I just realised the part was a bit forward from the spawn, and i was falling through the gap lol.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.