Need help detecting when a player stops touching a part

Hi, I was making a script where the player touches a part and is pushed upward using LinearVelocity, but I don’t want them to keep floating upward. I was planning to destroy the LinearVelocity when the player is no longer touching said part (which has no collision). But I feel that there’s a more efficient way to do this, though I’m not entirely sure what it is.

Here’s the current script which is parented inside the part.

--- I am so sorry for my spaghetti code

local FloatPart = script.Parent
local Debounce = 0

FloatPart.Touched:Connect(
	function(touch)
		if tick() >= Debounce then
			Debounce = tick() +1
			if touch.Parent:FindFirstChild("Humanoid") then
				local char = touch.Parent
				local player = game:GetService("Players"):GetPlayerFromCharacter(char)
				local LinearVelocity = Instance.new("LinearVelocity")
				local Attachment = Instance.new("Attachment")
				
				Attachment.Parent = player.Character.HumanoidRootPart
				Attachment.Name = "VelocityAttachment"

				LinearVelocity.MaxForce = 9999999
				LinearVelocity.VectorVelocity = Vector3.new(0,50,0)
				LinearVelocity.Attachment0 = player.Character.HumanoidRootPart.VelocityAttachment
				
				print(player.name)
				LinearVelocity.Parent = player.Character.HumanoidRootPart.VelocityAttachment
				
			end
		end
	end

)

I’d appreciate if someone could help me in figuring this out

thanks

1 Like

Use part.TouchEnded to detect when the player stops touching the part

14 Likes