How to stop a function (local script)

I want to learn how to disable a function. So once the next match starts, the UI doesn’t change for the person who is no longer the Killer. It only changes for the current killer.

[Example]

if player == "Killer" then
  Ui.MouseEnter:Connect(function()
  Ui.Textlabel.Text = "Mouse Entered"
  end)

  Ui.MouseLeave:Connect(function()
  Ui.Textlabel.Text = "Mouse Left"
  end)

else
  print(whatever)
end

If there’s a better way of doing this, knowing would also be beneficial.

You can make a local variable and when you need the function you set the variable to the function and when you need to stop it you use variable:Disconnect()

For example:

local Connection --the variable which will store the connection.

Connection = Ui.MouseEnter:Connect(function() --setting the variable to the connection. (you cannot directly set the variable, it has to be nil first)
  Ui.Textlabel.Text = "Mouse Entered"
end)

Connection:Disconnect() --prevents the connection to be fired again.

I hope this helped.

2 Likes
UI.MouseEnter:Once(function()

end)

Or you could do:

local Connection
Connection = UI.MouseEnter:Connect(function()
-- After you want it to not run again:
Connection:Disconnect()
end)
2 Likes

I didn’t even know about the :Once. The things you learn on this forum.

2 Likes

Thank you both for the support :slight_smile:

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