Disconnecting the code

  1. What do you want to achieve? Keep it simple and clear!
    I want to stop the code from running when a remote is called.

  2. What is the issue? Include screenshots / videos if possible!
    When I am trying to stop the code from running, it just continues to work.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tired using task.spawn, coroutine, connectioon:Disconnect()

So I am trying to make bandage system and run code that will run healing animation which after will heal the player when player is holding down their mouse and I want to stop it if player releases the mouse button, but it jsut continues the code

Local Script

Mouse.Button1Down:Connect(function()
	if not Ready or not Equipped or not BandagesOwned or BandagesOwned.Value < 1 then return end
	
	Event:FireServer()
	
end)
Mouse.Button1Up:Connect(function()
	if not Ready or not Equipped or not BandagesOwned or BandagesOwned.Value < 1 then return end
	
	EventStop:FireServer()
end)

Server Script

ConnectionRun = function(Player: Player,Type : string)
	local Character = Player.Character
	local Connection
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	
	Connection = Event2.OnServerEvent:Connect(function(Player)
		Connection:Disconnect()
		print("Are you alright?")
		Score = 0
	end)

	if not Character or not Humanoid or Humanoid.Health <= 0 then return false end

	for i = 1, math.huge do
		Score += 1
		print(Score)
		wait(1)
	end
end

Event.OnServerEvent:Connect(function(Player)
	ConnectionRun(Player)
end)

Result is always the same. It just continues to add up and print the score.

Did you mean something like this?

ConnectionRun = function(Player: Player,Type : string)
	local Character = Player.Character
	local Connection
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

	Event2.OnServerEvent:Once(function(Player)
		print("Are you alright?")
		Score = 0
	end)
	
	while not (Character and Humanoid) or Humanoid.Health <= 0 do
		Score += 1
		print(Score)
		task.wait(1)
	end
	
	return false
end

Event.OnServerEvent:Connect(ConnectionRun)

or this

ConnectionRun = function(Player: Player,Type : string)
	local Character = Player.Character
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	
	local IsWhile: boolean = true

	Event2.OnServerEvent:Once(function(Player)
		print("Are you alright?")
		
		IsWhile = false
		Score = 0
	end)
	
	if not (Character and Humanoid) or Humanoid.Health <= 0 then 
		return false 
	end
	
	while IsWhile do
		Score += 1
		print(Score)
		task.wait(1)
	end
end

Event.OnServerEvent:Connect(function(Player)
	ConnectionRun(Player)
end)

No, I would like to stop the CoonnectionRun function COMPLETELY when event2 is fired.
So again, I am trying to make a bandage system. When the player holds their mouse down, for example, for 5 seconds, it will heal that player, and if the player releases it if it didn’t complete that function, it stops the function completely and resets the timer when the player tries to heal themselves again.

Edit: Oh, I didn’t read the second code. Maybe that would do the trick. But why would connections and other stuff not work?

1 Like

That is, when event2 is fired the loop does not stop, and you want it to stop, right?

Yes, I want it to stop. I would prefer using one of the methods I tried mentiooned in first post. I just made that score thing just for the test.

Then why don’t you just specify a condition under which the loop breaks?

I said that I used this loop just for example. When I will solve my issue, I will just play the animation, and when the animation ends, it heals the player. If the disconnect runs, It will stop that animation. I should have done that before. I am sorry.

So I would do something like this:

ConnectionRun = function(Player: Player,Type : string)
	local Character = Player.Character
	local Connection
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
	
	local AnimationSelfHeal
	
	if Tool:FindFirstChild("SelfHealAnim") then
		AnimationSelfHeal = Humanoid:FindFirstChildWhichIsA("Animator"):LoadAnimation(Tool:FindFirstChild("SelfHealAnim"))
	end
	
	
	Connection = Event2.OnServerEvent:Connect(function(Player)
		Connection:Disconnect()
		print("Are you alright?")
		AnimationSelfHeal:Stop()
	end)

	if not Character or not Humanoid or Humanoid.Health <= 0 then return false end
	
	AnimationSelfHeal:Play()
	AnimationSelfHeal.Ended:Wait()
	Humanoid.Health = Humanoid.MaxHealth
end

Event.OnServerEvent:Connect(function(Player)
	ConnectionRun(Player)
end)

Sorry, but I don’t quite understand what exactly you want. Can you explain what you mean by


Also how would you use coroutine or task.spawn to implement your goal?

I edited my last message, it contains what exactly I want to do, I just didn’t want to make stuff complicated but I guess I have to.

and with coroutines and task.spawn I used this two messages:

try using a variable and setting it to false when you want connectionrun to stop

Okay, so you want the Humanoid to not gain Health when event2 fires, right? Because I don’t think the player will have time to release the mouse button in such a short time.

ConnectionRun = function(Player: Player,Type : string)
	local Character = Player.Character
	local Connection: RBXScriptSignal?
	local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

	local AnimationSelfHeal

	if Tool:FindFirstChild("SelfHealAnim") then
		AnimationSelfHeal = Humanoid:FindFirstChildWhichIsA("Animator"):LoadAnimation(Tool:FindFirstChild("SelfHealAnim"))
	end

	Connection = Event2.OnServerEvent:Once(function(Player)
		print("Are you alright?")
		AnimationSelfHeal:Stop()
	end)

	if not Character or not Humanoid or Humanoid.Health <= 0 then return false end

	AnimationSelfHeal:Play()
	AnimationSelfHeal.Ended:Wait()
	
	if Connection.Connected then
		Connection:Disconnect()
		Humanoid.Health = Humanoid.MaxHealth
	end
end

Event.OnServerEvent:Connect(function(Player)
	ConnectionRun(Player)
end)

Okay, sorry, but I’ve managed to make it work with some workarounds. Here’s my result code. I would like to know if I am doing something wrong that would be appreciated

Local

Mouse.Button1Down:Connect(function()
	if not Equipped or not BandagesOwned or BandagesOwned.Value < 1 then return end
	
	Event:FireServer("Self")
	
end)
Mouse.Button1Up:Connect(function()
	if not Equipped or not BandagesOwned or BandagesOwned.Value < 1 then return end
	
	EventStop:FireServer()
end)

Server

local Thread: thread = nil

local StoredTracks = {}

Event.OnServerEvent:Connect(function(Player: Player,Type : string)
	print("fired")
	Thread = task.spawn(function()
		local Character = Player.Character
		local Connection
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

		if not Character or not Humanoid or Humanoid.Health <= 0 or Humanoid.Health >= Humanoid.MaxHealth then return false end

		AnimatinLoadedHealSelf:Play()
		print("Played", 3)
		AnimatinLoadedHealSelf.Ended:Wait()
		print("Done")
		Humanoid.Health = Humanoid.MaxHealth
	end)
	StoredTracks[Player.Name] = AnimatinLoadedHealSelf
end)

Event2.OnServerEvent:Connect(function(Player)
	if Thread then task.cancel(Thread) end
	if StoredTracks[Player.Name] then
		StoredTracks[Player.Name]:Stop()
		StoredTracks[Player.Name] = nil
	end
	print("Stopped")
end)

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