[darkrp] How to make cop cars despawn when switching to a different job.

Lately I have been having difficulty getting police vehicles to despawn when you switch from cp. I have looked all around for some help on this but I can’t find anything that is helpful and I am not sure how to do this. Can anyone help me?


for k, v in pairs( ents.GetAll() ) do
	if v:IsValid() and v:GetClass == "carclass" and v:GetOwner() == playermeta then
  v:Remove()
end
end

Try using something like that for when they switch jobs.

Thanks so much. One question: I don’t want to sound dumb but where can I put this?

Here’s something that’s relatively plug and play. You have to change the two variables at the top to work with the CP team name and the class of the vehicle that your server uses.

[lua]
hook.Add(“OnPlayerChangedTeam”, “DeleteCPCar”, function(ply, before, after)
local CPClass = SOME_CP_CLASS
local PatrolCarName = “CP_PATROL_CAR_NAME”

if before == CPClass then
for _, v in pairs(ents.GetAll()) do
if v:IsValid() and v:GetClass() == PatrolCarName and v:GetOwner() == ply then
v:Remove()
end
end
end
end)
[/lua]

Put it into a serverside file (For example autorun/server/deletecars.lua).

That doesn’t seem to be working for me This is what I got and it should be working


hook.Add("OnPlayerChangedTeam", "DeleteCPCar", function(ply, before, after)
local CPClass = TEAM_POLICE
local PatrolCarName = "sgmcrownviccvpi"

  if before == CPClass then
    for _, v in pairs(ents.GetAll()) do
      if v:IsValid() and v:GetClass() == PatrolCarName and v:GetOwner() == ply then
        v:Remove()
      end
    end
  end
end)

Wait, that looks the exact same, please don’t tell me you copied his code and didnt change the CPClass and PatrolCarName the first time…

I did change it I changed the team to TEAM_POLICE and the car to to the car name I am using don’t you see that?

I have tried everything with this and I can’t get it to work :frowning:

bump can anyone help me with this?

What addon for cars are you using?, what’s the cop job code?

TDM cars and for the police cars SGM the car name I want to use is sgmcrownviccvpi and the Job code for my police is this.


TEAM_POLICE = DarkRP.createJob("Civil Protection", {
	color = Color(25, 25, 170, 255),
	model = {"models/player/police.mdl", "models/player/police_fem.mdl"},
	description = [[The protector of every citizen that lives in the city.
		You have the power to arrest criminals and protect innocents.
		Hit a player with your arrest baton to put them in jail.
		Bash a player with a stunstick and they may learn to obey the law.
		The Battering Ram can break down the door of a criminal, with a warrant for their arrest.
		The Battering Ram can also unfreeze frozen props (if enabled).
		Type /wanted <name> to alert the public to the presence of a criminal.]],
	weapons = {"arrest_stick", "unarrest_stick", "weapon_glock2", "stunstick", "door_ram", "weaponchecker"},
	command = "cp",
	max = 4,
	salary = 65,
	admin = 0,
	vote = true,
	hasLicense = true,
	help = {
		"Please don't abuse your job",
		"When you arrest someone they are auto transported to jail.",
		"They are auto let out of jail after some time",
		"Type /warrant [Nick|SteamID|Status ID] to set a search warrant for a player.",
		"Type /wanted [Nick|SteamID|Status ID] to alert everyone to a wanted suspect",
		"Type /unwanted [Nick|SteamID|Status ID] to clear the suspect",
		"Type /jailpos to set the jail position"
	}
})

[lua]hook.Add(“OnPlayerChangedTeam”, “asdf”, function(ply)
for k,v in pairs(ents.FindByClass(“YOURENTITY”)) do
if v.allowed and type(v.allowed) == “table” and table.HasValue(v.allowed, t) then continue end
if v.SID == ply.SID then v:Remove() end
end

end)[/lua]

From Falco - “put this in the darkrpmod addon in some module.”

Thank you that helped a lot! :smiley: I can’t thank you enough!

Its not working for me, someone knows a fix ? :confused: Would be very thankfull!


hook.Add("OnPlayerChangedTeam", "asdf", function(ply)
        for k,v in pairs(ents.FindByClass("Bus")) do
            if v.allowed and type(v.allowed) == "table" and table.HasValue(v.allowed, t) then continue end
            if v.SID == ply.SID then v:Remove() end
        end

end)

[editline]23rd February 2014[/editline]

Fixxed it for myself