Changing color of vehicle based on team


local entities = ents.GetAll()
local ply = LocalPlayer()


Teams={}
Teams[Citizen] = {TEAM_CITIZEN}
Teams[CP] ={TEAM_POLICE}

for k, v in pairs(entities) do
	if v:IsVehicle == true and v:GetOwner == Teams[Citizen]  then
		v:SetColor(Color(255,0,0))
		
	else 
		v:SetColor(Color(0,0,255))
	end
	 
end

Ok I need some help with this little code. I am very new to lua so please bare with me. I am not looking to be spoon fed, I just want to be pointed in a direction. I know my problem is with v:GetOwner == Teams[Citizen] and I want to know what I should put there instead! Thank you for the help!

  1. This code should be Serverside, so remove LocalPlayer part
  2. You’re missing how tables works:
    Teams[Citizen] is incorrect, you might meant Teams[“Citizen”]
    Because that’s how indexes works, via strings and numbers, although you can play with variables, but you have to define them
  3. You have to add () to functions:
    v:IsVehicle is incorrect, is v:IsVehicle()
  4. You don’t need to verify true, just do v:IsVehicle()
  5. You have to run this code inside a hook, most possibly when a car is created

Ok thank you this was very helpful! Do I have the v:GetOwner == Teams[Citizen] correct? Like will that check to see if the player is in that job?

Nope, read point 2 and 3

v:GetOwner() == Teams[“Citizen”]

so that would work?

[editline]23rd November 2016[/editline]

or do I have to do something specifically to check the job of the owner?

Yeah, be sure to modify the other table too…Just remember that it has to be run sometimes

Alright thank you

[editline]23rd November 2016[/editline]

I realized it would probably be a lot on the server check for all entities to find the vehicles so I will use the PlayerSpawnedVehicle hook instead like you said.

[editline]23rd November 2016[/editline]

Got it to work! Thanks man!