How to stop an :Connect(Function)?

Hello. I basically want to be able to stop this function when the animation is done playing so it doesn’t keep repeating itself. I already know that someone is gonna be like “use a variable,” which would normally be good advice, but in this case I already tried it and it didn’t work. I basically just need like a return or something for this function to get it to stop running without affecting any other part of the script.

Here’s the function I need to stop:

monsterhumroot.Touched:Connect(function(touch)
	if touch == playerbeingchased.HumanoidRootPart then
			local previouswalkspeed = monsterhum.WalkSpeed
			monster:FindFirstChild("Animate").Enabled = false
			task.wait()
		    for i,v in pairs(humanoid:GetPlayingAnimationTracks()) do 
				v:Stop() 
			end
			task.wait()
			killdance:Play()
			task.wait()
			playerbeingchased.Humanoid.Health = 0
			monsterhum.WalkSpeed = 0
			killdance.Ended:Connect(function()
				monsterhum.WalkSpeed = previouswalkspeed
               --I want the stop to be here. If it would just stop the .ended function, I can use a 
               --variable to tell when it ends and do it 
			end)
           --here instead.
		end
	end)

Thanks!

killdance.Ended:Once
1 Like

The Connect method returns an RBXScriptConnection which you can assign to a variable and use the Disconnect method on at any point after.

local connection
connection = instance.Event:Connect(function()
    conneciton:Disconnect()
end)

You can also just use the Once method if you only need it to run once.

2 Likes

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