How can I make a piece of code run when an object has been destroyed? I tried using if functions but that only runs once. I dont want the if function to run every tenth of a second just so it can always be checking. I don't think there is an event for this either. Does anyone have any ideas? P.S I'm trying to check if a player has left.
Firstly, you should use an event if you care about the state of something only when it changes. This is opposed to using something like a loop, which continuously checks the state of something even when it is the same -- not always the best option.
As a side note: the phrase "if function" you're referring to is not quite accurate. "if" is a statement, not a function. Functions in programming are much different than statements. Statements are more fundamental and focus only on conditions.
Generally, you can check the parent state of an object once it has changed by using the ChildRemoving
or DescendantRemoving
event on the parent object. You could also use the Changed
event and check if the specific property changed was the parent, but that would be unnecessary in this case. Usually, you don't have to use the Changed
event because there are more specific events dedicated to handling the appropriate procedure. Here's an example:
workspace.DescendantRemoving:Connect(object) -- code that handles removing of object: object -- from the workspace. end)
Since you specified the case in which you plan on using this information, I have a better suggestion. Use the PlayerRemoving
event (which is a member of the Players service) that handles some process whenever a player leaves your game. Here's an example:
local PlayersService = game:GetService("Players") PlayersService.PlayerRemoving:Connect(function(player) -- code that handles removing of player: player -- from the players service end)
function Add(x, y) -- makes a function x, y x and y means nothing its basicly nothing print "finding sum" -- prints finding sum return (x, y) -- destroys/disables the function x, y end add(1, 3) -- makes x,y avaible again
or
function Add() -- makes a function if 1 == 1 then -- if 1 is equal with 1 then print ("oke") -- print oke Destroy:Add -- destroys add im not sure its like this end end
i think its like this if it isnt tell me and i will reply with a diff way