How to Check if car is already spawned?

Hello,

I have made this npc for cops that they can spawn a car.
How would i check if they say yes to spawn if its already spawned.
And if it is then spawn a new car and remove the old one.
Anyone an idea.(setNWInt ?)
Here are my codes.

cl_init.lua

[lua]
include(‘shared.lua’) – At this point the contents of shared.lua are ran on the client only.

function NPCShopMenu()

local DFrame1 = vgui.Create(‘DFrame’)
DFrame1:SetSize(320, 117)
DFrame1:SetPos(6, 24)
DFrame1:Center()
DFrame1:SetTitle(‘Cop car’)
DFrame1:SetSizable(true)
DFrame1:SetDeleteOnClose(false)
DFrame1:MakePopup()
DFrame1:SetBackgroundBlur(true)

local DPanel1 = vgui.Create(‘DPanel’)
DPanel1:SetParent(DFrame1)
DPanel1:SetSize(309, 86)
DPanel1:SetPos(6, 24)

local SpawnIcon1 = vgui.Create(‘SpawnIcon’)
SpawnIcon1:SetParent(DPanel1)
SpawnIcon1:SetPos(10, 12)
SpawnIcon1:SetToolTip(‘Cop’)
SpawnIcon1:SetModel(‘models/Combine_Soldier.mdl’)

local DLabel1 = vgui.Create(‘DLabel’)
DLabel1:SetParent(DPanel1)
DLabel1:SetPos(85, 18)
DLabel1:SetText(‘Do you want to spawn your service car?’)
DLabel1:SizeToContents()

local DButton1 = vgui.Create(‘DButton’)
DButton1:SetParent(DPanel1)
DButton1:SetSize(70, 25)
DButton1:SetPos(105, 48)
DButton1:SetText(‘Yes’)
DButton1.DoClick = function()
DFrame1:Close()
chat.AddText(Color(255,255,128), "Cop: ",Color(255,255,255), “Your car has been spawned outside of the nexus.” )
LocalPlayer():ConCommand(“spawn_copcar”) end

local DButton2 = vgui.Create(‘DButton’)
DButton2:SetParent(DPanel1)
DButton2:SetSize(70, 25)
DButton2:SetPos(190, 48)
DButton2:SetText(‘No’)
DButton2.DoClick = function()
DFrame1:Close() end

-- We can also do anything else the client can do, like using the chat!
chat.AddText(Color(255,255,128), "Cop: ",Color(255,255,255), "Welcome, how can I help you?" )

end

usermessage.Hook(“ShopNPCUsed”, NPCShopMenu) --Hook to messages from the server so we know when to display the menu.
[/lua]

init.lua

[lua]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

function ENT:Initialize( )

self:SetModel( "models/Combine_Soldier.mdl" ) 
self:SetHullType( HULL_HUMAN ) 
self:SetHullSizeNormal( )
self:SetNPCState( NPC_STATE_SCRIPT )
self:SetSolid(  SOLID_BBOX ) 
self:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD ) 
self:SetUseType( SIMPLE_USE ) 
self:DropToFloor()

self:SetMaxYawSpeed( 90 ) 

local PhysAwake = self.Entity:GetPhysicsObject( )
if PhysAwake:IsValid( ) then
	PhysAwake:Wake( )
end 

end

function ENT:OnTakeDamage()
return false
end

function ENT:AcceptInput( Name, Activator, Caller )

if Name == "Use" and Caller:IsPlayer() then
	umsg.Start("ShopNPCUsed", Caller) 
	umsg.End() 
	
end

end

function SpawnCarCop(ply, ent)
local CopCar = ents.Create(“prop_vehicle_jeep”)
CopCar:SetModel(“models/sickness/lcpddr.mdl”)
CopCar:SetKeyValue(“vehiclescript”, “scripts/vehicles/ecpd01.txt”)
CopCar:SetPos(Vector(-6208.3750, -8995.6250, 61.9375))
–CopCar.Owner = ply
–CopCar:Own(ply)
CopCar:Spawn()
CopCar:Activate()
end
concommand.Add(“spawn_copcar”, SpawnCarCop)

[/lua]

Store the car in a variable in the players table, then check this variable when you send it to the client.

cl_init.lua
[lua]
include(‘shared.lua’) – At this point the contents of shared.lua are ran on the client only.

function NPCShopMenu( um )

local spawnedalready = um:ReadBool() – first we need to read the stuff the server sent us
– if the player already had a car out “spawnedalready” would now be true
– if they didn’t, it would be false

local DFrame1 = vgui.Create(‘DFrame’)
DFrame1:SetSize(320, 117)
DFrame1:SetPos(6, 24)
DFrame1:Center()
DFrame1:SetTitle(‘Cop car’)
DFrame1:SetSizable(true)
DFrame1:SetDeleteOnClose(false)
DFrame1:MakePopup()
DFrame1:SetBackgroundBlur(true)

local DPanel1 = vgui.Create(‘DPanel’)
DPanel1:SetParent(DFrame1)
DPanel1:SetSize(309, 86)
DPanel1:SetPos(6, 24)

local SpawnIcon1 = vgui.Create(‘SpawnIcon’)
SpawnIcon1:SetParent(DPanel1)
SpawnIcon1:SetPos(10, 12)
SpawnIcon1:SetToolTip(‘Cop’)
SpawnIcon1:SetModel(‘models/Combine_Soldier.mdl’)

local DLabel1 = vgui.Create(‘DLabel’)
DLabel1:SetParent(DPanel1)
DLabel1:SetPos(85, 18)
DLabel1:SetText(‘Do you want to spawn your service car?’)
DLabel1:SizeToContents()

local DButton1 = vgui.Create(‘DButton’)
DButton1:SetParent(DPanel1)
DButton1:SetSize(70, 25)
DButton1:SetPos(105, 48)
DButton1:SetText(‘Yes’)
DButton1.DoClick = function()

if spawnedalready then -- if they've already got a car spawned
	-- Derma_Query is a quick and cheap way of making a Yes/No box
	-- Search Derma_ on the Funcion Search for more (they're quite useful)
	-- the function takes the arguments Derma_Query( "text to display in the box", "title of the box", "text on the first button", function on the first button, etc )
	Derma_Query( "You already have a car spawned, are you sure you wish to respawn it?", "Existing car detected", "Yes", function()
		DFrame1:Close() -- this is the function for the "Yes" button, so if they click yes it will respawn their car
		chat.AddText(Color(255,255,128), "Cop: ",Color(255,255,255), "Your car has been spawned outside of the nexus." )
		LocalPlayer():ConCommand("spawn_copcar")
	end, "No", function() DFrame1:Close() end ) -- here is the function for the "No" button, we'll just close the window here
	
	return -- return it here so stuff doesn't happen twice
end
DFrame1:Close() -- this gets run as normal as they didn't have a car
chat.AddText(Color(255,255,128), "Cop: ",Color(255,255,255), "Your car has been spawned outside of the nexus." )
LocalPlayer():ConCommand("spawn_copcar") 

end

local DButton2 = vgui.Create(‘DButton’)
DButton2:SetParent(DPanel1)
DButton2:SetSize(70, 25)
DButton2:SetPos(190, 48)
DButton2:SetText(‘No’)
DButton2.DoClick = function()
DFrame1:Close() end

-- We can also do anything else the client can do, like using the chat!
chat.AddText(Color(255,255,128), "Cop: ",Color(255,255,255), "Welcome, how can I help you?" )

end

usermessage.Hook(“ShopNPCUsed”, NPCShopMenu) --Hook to messages from the server so we know when to display the menu.
[/lua]

init.lua
[lua]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

function ENT:Initialize( )

self:SetModel( "models/Combine_Soldier.mdl" ) 
self:SetHullType( HULL_HUMAN ) 
self:SetHullSizeNormal( )
self:SetNPCState( NPC_STATE_SCRIPT )
self:SetSolid(  SOLID_BBOX ) 
self:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD ) 
self:SetUseType( SIMPLE_USE ) 
self:DropToFloor()
  
self:SetMaxYawSpeed( 90 ) 
  
local PhysAwake = self.Entity:GetPhysicsObject( )
if PhysAwake:IsValid( ) then
    PhysAwake:Wake( )
end

end

function ENT:OnTakeDamage()
return false
end

function ENT:AcceptInput( Name, Activator, Caller )

if Name == "Use" and Caller:IsPlayer() then
    umsg.Start("ShopNPCUsed", Caller) -- the trick here is, if the car has been removed or doesn't exist, then it will no longer be a valid entity
         umsg.Bool( ValidEntity( caller.copcar ) ) -- so we send that to the client (will be true if they have a car, false if they don't)
    umsg.End() 
      
end

end

function SpawnCarCop(ply, ent)
if ply.copcar and ValidEntity( ply.copcar ) then ply.copcar:Remove() end
local CopCar = ents.Create(“prop_vehicle_jeep”)
CopCar:SetModel(“models/sickness/lcpddr.mdl”)
CopCar:SetKeyValue(“vehiclescript”, “scripts/vehicles/ecpd01.txt”)
CopCar:SetPos(Vector(-6208.3750, -8995.6250, 61.9375))
– CopCar.Owner = ply
–CopCar:Own(ply)
CopCar:Spawn()
CopCar:Activate()
ply.copcar = CopCar – add the car to the players table, so we can check for it later
end
concommand.Add(“spawn_copcar”, SpawnCarCop)
[/lua]

I get this error



[lua\entities
pc_test\cl_init.lua:45] ')' expected (to close '(' at line 41) near 'end'


i tried to figure it out but i am new to derma query

[editline]11th August 2011[/editline]

I fixed this error but now i get this.(btw thanks for your help)
it has to do something with this umsg.Bool( ValidEntity( caller.copcar ) )



Error - Starting new message without ending the old one!
[lua\entities
pc_test\init.lua:35] attempt to index global 'caller' (a nil value)


[editline]11th August 2011[/editline]

LOL
umsg.Bool( ValidEntity( caller.copcar ) )
must be
umsg.Bool( ValidEntity( Caller.copcar ) )

Caller with a capital C , and it works ty

[editline]11th August 2011[/editline]

Can you comment the things you added so i can learn from it.
I am learing lua thats why i ask :blush:
( it also works when the car is destroyed no derma query nice :open_mouth: )

[editline]11th August 2011[/editline]

Drakehawke,

Do you know how i remove the car on a DarkRP Job change ?

Sorry about that, I’m used to using lowercase C there :v:

I’ve edited the previous post to include comments explaining some of the code.

And you want to edit DarkRP/gamemode/player.lua

Find:

[lua]function meta:ChangeTeam( t, force )[/lua]

And you’ll need to add into that function, something like:

[lua]if self.copcar and ValidEntity( self.copcar ) then self.copcar:Remove() end[/lua]