Make custom entity disappear when changing jobs

So I have this Casino manager job that allows the player to place their own casino entities like slot machine etc but when they change their job the entities stay. Is there a way to make custom entities disappear when changing jobs?

You can use the PlayerChangedTeam event for iterating through all entities of a specific class with ents.FindByClass or use ents.GetAll for a wider search.

And where would I put said code? Could you maybe provide an example with the following?

DarkRP.createEntity("Piano", {
    ent = "gmt_instrument_piano",
    model = "models/fishy/furniture/piano.mdl",
    price = 1000,
    max = 1,
    cmd = "buypiano",
    customCheck = function(ply) return
        table.HasValue({TEAM_PIANIST}, ply:Team())
    CustomCheckFailMsg = "",

You need to create a server side script (which you can put in an addon or a DarkRP module). For your piano entity, here is what I would have done :

hook.Add( "PlayerChangedTeam", "unique_name", function( ply, oldTeam, newTeam )
	if oldTeam == newTeam then return end

	for _, v in ipairs( ents.FindByClass( "gmt_instrument_piano" ) ) do
		if v:GetCreator() == ply then
end )
1 Like