Connection Scripting Help

  1. What do you want to achieve? Keep it simple and clear!

    I want to make a walking animation and a running animation at different states of an enemy ( Such as it walks normally but when it sees you it runs )

  2. What is the issue? Include screenshots / videos if possible!

    I’m using humanoid.Running functions to detect when to play the animations and I’m trying to use the :Disconnect function to disconnect the walking function and temporarily replace it with the running function, and vice versa but it doesn’t break the connection.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have looked everywhere, Dev Forum, Google, Youtube, I even tried using Disconnect on a new baseplate and it worked however it doesn’t on my game. Nothing seemed to help.

This is my code:

while task.wait(0.5) do 
	
	Connection = humanoid.Running:Connect(function()
		if walkTrack.IsPlaying == false then			
			
			warn("Walking")			
			walkTrack:Play()
			Connection:Disconnect() -- This one seems to disconnect properly
			
		end
		
	end)

---------- This section is irrelevent but if you need to see it just ask. 
---------- It's just some raycasting stuff


if hit.Parent:FindFirstChild("Humanoid") then -- detects player with ray
				if walkTrack.IsPlaying == true then
					walkTrack:Stop() -- stops monster from using the walking animations
				end
				
				
				humanoid.PlatformStand = true
				
				task.wait(4)
				
				humanoid.PlatformStand = false
				script.Parent.Humanoid.WalkSpeed = 25
				roar:Play()
				while task.wait(1) do
					
					Connection2 = humanoid.Running:Connect(function()
						warn("Run Checker Ran")
						-- this is the problem function
 -- It checks if the monster humanoid is running then plays an animation
						if runTrack.IsPlaying == false then
							
							print("Running")
							runTrack:Play()						
							Connection2:Disconnect() 
							--it disconnects but the function still repeats for about 1000 times
						end						
						
					end)
if (torso.Position - script.Parent.PrimaryPart.Position).Magnitude >= 200 or not 
-- this checks if the monster killed the player,  so the monster should stop running
hit.Parent:FindFirstChild("Humanoid") then
						warn("Player gone or dead")
						
						

						script.Parent.Humanoid.WalkSpeed = 10
						runTrack:Stop()
						roar:Stop()
						break
					end
					-- then the while loop at the top repeats because if the running function 
-- disconnects that means the monster killed the player and reset
					end
				end
			end
		end
	end
end

This has been extremely frustrating, thanks to anyone who responds in advance!

The reason why it repeats 1000 times is because you have it in a while loop where each time you create a new connection why dont you use :PropertyChangedSignal(Property) which gets called every time the Property changes instead of having a while loop

1 Like

Sorry, I didn’t realize but a bit of code was left out let me fix it…
There’s is a break that should stop the loop so it shouldn’t recreate the event

Sorry about the messy script, this project has been going on for quite a while and as you probably know as time goes you get better at scripting.

Unfortunately it didn’t work, it did the same thing the second while loop broke and the event still kept repeating. Thanks for your help though!

Sorry, I just went over it again I figured out what was wrong with your help. It looped through the event creating it over 1000 times. I thought I could just disconnect it but it was disconnecting 1 of 1000 of set events. Creating what I think is called a memory leak. Thank you for your help!