NPC Shop HELP :)

Every time I spawn a vehicle it is spawned unowned and i can spawn how ever many i want is there a way to limit so only one car per person at a time and make it spawned already owned by the person who bought it.

I get this error: http://imgur.com/vxohaAN

Server.lua
[lua]
if not SERVER then return end

util.AddNetworkString( “npcshop_senddata” )

function SpawnnpcShop()
local shop = ents.Create(“npc_shop”)
shop:SetPos( Vector(-2138, -690, -131) )
shop:SetAngles( Angle(1, -88, 0) )
shop:Spawn()
shop:DropToFloor()
end

hook.Add(“InitPostEntity”, “SpawnnpcShop”, SpawnnpcShop)

hook.Add(“EntityTakeDamage”, “PReventNPCfromdying”, function( target, dmginfo )
if target:IsNPC() and target:GetClass() == “npc_shop” then
dmginfo:ScaleDamage(0)
end
end)

local function IsVIP(ply)
local isvip = false
for k,v in pairs(NPCSHOP.UserGroups) do
if ply:GetUserGroup() == v then
isvip = true
break
end
end
return isvip
end

//Utilizes darkrp’s notify
local function Notify(ply, text)
if not IsValid(ply) then return end
umsg.Start("_Notify", ply)
umsg.String(text)
umsg.Short(0)
umsg.Long(4)
umsg.End()
end

local function SaveVehicles()
local str = util.TableToJSON(NPCSHOP.PlayerVehicles)
file.Write( “npcshopsaves.txt”, str )
end
local function LoadVehicles()
local str = file.Read( “npcshopsaves.txt”, “DATA” ) or “
NPCSHOP.PlayerVehicles = util.JSONToTable(str)
end
LoadVehicles()
function SendVehicles(ply)
local sid = ply:SteamID()
local cars = NPCSHOP.PlayerVehicles[sid] or {}

net.Start("npcshop_senddata")
	net.WriteTable(cars)
net.Send(ply)

end

hook.Add(“PlayerInitialSpawn”, “plyinitspawnnpcshop”, function(ply)
timer.Simple(2, function()
SendVehicles(ply)
end)
end)

local meta = FindMetaTable(“Player”)
function meta:AddVehicle( class )
local sid = self:SteamID()
if not NPCSHOP.PlayerVehicles[sid] then NPCSHOP.PlayerVehicles[sid] = {} end

NPCSHOP.PlayerVehicles[sid][class] = true
SaveVehicles()
SendVehicles(self)

end

local function SpawnVehicle(ply, class)
if not NPCSHOP.VehicleLookup[class] then return end
if not NPCSHOP.Vehicles[NPCSHOP.VehicleLookup[class]] then return end

if IsValid(ply.currentcar) then
	local d = ply.currentcar:GetDriver()
	if IsValid(d) and d != ply then
		Notify(d, "The owner of this car has removed it!")
	end
	ply.currentcar:Remove()
end

local vehicle = list.Get("Vehicles")[class]
if not vehicle then return end

local car = ents.Create(vehicle.Class)
	if not car then return end
	
	car:SetModel(vehicle.Model)
	
	if vehicle.KeyValues then
		for k, v in pairs(vehicle.KeyValues) do
			car:SetKeyValue(k, v)
		end
	end

	car.VehicleName = class
	car.VehicleTable = vehicle
	car.Owner = ply

local carspawns = NPCSHOP.CarSpawn[game.GetMap()]
local pos = carspawns[1].pos
local ang = carspawns[1].ang
for k,tbl in pairs(carspawns) do
	local p,a = tbl.pos, tbl.ang
	local entslist = ents.FindInBox(Vector(p.x + 100, p.y + 100, p.z - 20), Vector(p.x - 100, p.y - 100, p.z + 150))
	if #entslist == 0 then
		pos = p
		ang = a
		break
	end
end

car:SetPos(pos)
car:SetAngles(ang)

car:Spawn()
car:Activate()
car.SID = ply.SID
car.ClassOverride = vehicle.Class
if vehicle.Members then
	table.Merge(car, vehicle.Members)
end
car:own(ply)
gamemode.Call("PlayerSpawnedVehicle", ply, car)

ply.currentcar = car

end

concommand.Add("_npcshopbtnclick", function(ply, _, args)
if #args != 1 then return end
if not IsValid(ply) then return end

if ply:GetPos():Distance(ents.FindByClass("npc_shop")[1]:GetPos()) > 80 then return end

local class = args[1]
if not NPCSHOP.VehicleLookup[class] then return end
if not NPCSHOP.Vehicles[NPCSHOP.VehicleLookup[class]] then return end

local cltbl = NPCSHOP.Vehicles[NPCSHOP.VehicleLookup[class]]

if #cltbl.job > 0 then
	if not table.HasValue(cltbl.job, ply:Team()) then
		Notify(ply, "You're not in the correct job to spawn/purchase this!")
		return
	end
end

if ply:OwnsVehicle(class) then
	SpawnVehicle(ply, class)
	return
end

if cltbl.viponly and not IsVIP(ply) then
	Notify(ply, "This vehicle is VIP only!")
	return
end

if not ply:canAfford(cltbl.price) then
	Notify(ply, "You can not afford that!")
	return
end

ply:addMoney(-cltbl.price)
Notify(ply, "You've bought the '" .. cltbl.name .. "' for "..(CUR or "$")..(cltbl.price).."!")
ply:AddVehicle(class)

umsg.Start("_updatenpcshopgui", ply)
	umsg.String(class)
umsg.End()

end)

// DarkRP doesn’t give me any way to check for job changes, then this shit is needed!

local bkp = meta.SetTeam
meta.SetTeam = function(self, job)
bkp(self, job)

if IsValid(self.currentcar) then
	local class = self.currentcar.VehicleName
	local cltbl = NPCSHOP.Vehicles[NPCSHOP.VehicleLookup[class]]
	
	if #cltbl.job > 0 then
		if not table.HasValue(cltbl.job, self:Team()) then
			self.currentcar:Remove()
			Notify(self, "Your current car isn't allowed for your new job!")
			return
		end
	end
end

end[/lua]

Cinfig.lua
[lua]/*
Only change stuff in here!
*/
NPCSHOP = NPCSHOP or {}
NPCSHOP.CarSpawn = {}
NPCSHOP.NPCSpawn = {}

/*
Edit stuff below this line


*/
// Model of the NPC, can be changed to whatever you want, as long as it follows this path of models/<modelname>.mdl
NPCSHOP.NPCModel = “models/barney.mdl”

//Position of the NPC, in YOUR console, type getpos to get positioning co-ordinates

NPCSHOP.NPCSpawn[“rp_downtown_v4c_v2”] = {
pos = Vector(-2138, -690, -131), //Same co-ordinates used for the config file
ang = Angle(1, -88, 0)
}

//Position for the carspawn

NPCSHOP.CarSpawn[“rp_downtown_v4c_v2”] = {
{
pos = Vector(-2295, -470, -131),
ang = Angle(-2, -91, 0)
},
}

//Which ULX usergroups are considered “VIP”
NPCSHOP.UserGroups = {“vip”, “admin”}

/*
Add vehicles here.

The jobrestriction is abit wonky, in darkrp you can just do like {TEAM_POLICE, TEAM_CHIEF, TEAM_MAYOR} but it doesn’t really work the same here.
Jobrestriction is put with {1,2,3} where 1, 2 and 3 are the team numbers of those teams, in order to get the team numbers, just go ingame, open console
using F11 and type “lua_run_cl print(TEAM_CITIZEN)” which will give you the team number for citizen. You can also get the teamnumber of your current
team by typing “lua_run_cl print(LocalPlayer():Team())”
If you add or remove darkrp teams, this process (in some cases) have to be done again!

NPCSHOP.AddVehicle(name, class, model, price, jobrestriction, viponly)
name: Name of the vehicle in the npcshop menu
class: Vehicle-class of the vehicle. Use “rp_getvehicles” to find these.
model: Model which will appear in the npcshop menu
price: How much it costs
jobrestriction: What jobs will be able to buy this (type nil if its available to all jobs)
viponly: Is this vehicle only available for VIP’s? true/false
Display name > Class (rp_getvehicles in console) > Model directory > Price > Jobs > VIP/Not VIP - False = Not VIP, True = VIP
*/
NPCSHOP.AddVehicle(“audir8”, “audir8tdm”, “models/tdmcars/audir8.mdl”, 50000, nil, false)
NPCSHOP.AddVehicle(“350z”, “350ztdm”, “models/tdmcars/350z.mdl”, 100, nil, false)
AddCustomVehicle(“69 camaro”, “69camarotdm”, “models/tdmcars/69camaro.mdl” , 2000, nil, false )
AddCustomVehicle(“997 GT3”, “997gt3tdm”, “models/tdmcars/997gt3.mdl” , 2000, nil, false )
AddCustomVehicle(“Scirocco”, “sciroccotdm”, “models/tdmcars/scirocco.mdl” , 2000, nil, false )
AddCustomVehicle(“mercedes sl65 AMG”, “sl65amgtdm”, “models/tdmcars/sl65amg.mdl” , 2000, nil, false )
AddCustomVehicle(“Dodge Charger srt8”, “chargersrt8tdm”, “models/tdmcars/chargersrt8.mdl” , 2000, nil, false )
AddCustomVehicle(“cayenne”, “cayennetdm”, “models/tdmcars/cayenne.mdl” , 2000, nil, false )
AddCustomVehicle(“Mitsubishi EVOx”, “mitsu_evoxtdm”, “models/tdmcars/mitsu_evox.mdl” , 2000, nil, false )
AddCustomVehicle(“Ford Focus”, “focusrstdm”, “models/tdmcars/focusrs.mdl” , 2000, nil, false )
AddCustomVehicle(“AudiTT”, “auditttdm”, “models/tdmcars/auditt.mdl” , 2000, nil, false )
AddCustomVehicle(“DBS”, “dbstdm”, “models/tdmcars/dbs.mdl” , 2000, nil, false )
AddCustomVehicle(“242 Turbo”, “242turbotdm”, “models/tdmcars/242turbo.mdl” , 2000, nil, false )
AddCustomVehicle(“GMC Van”, “gmcvantdm”, “models/tdmcars/gmcvan.mdl” , 2000, nil, false )
AddCustomVehicle(“BMW M5 E60”, “bmwm5e60tdm”, “models/tdmcars/bmwm5e60.mdl” , 2000, nil, false )
AddCustomVehicle(“c4”, “c4tdm”, “models/tdmcars/cit_c4.mdl” , 2000, nil, false )
AddCustomVehicle(“c12”, “c12tdm”, “models/tdmcars/zondac12.mdl” , 2000, nil, false )
AddCustomVehicle(“ceed”, “ceedtdm”, “models/tdmcars/kia_ceed.mdl” , 2000, nil, false )
AddCustomVehicle(“Ferrari 512”, “ferrari512trtdm”, “models/tdmcars/ferrari512tr.mdl” , 2000, nil, false )
AddCustomVehicle(“Ferrari 250”, “ferrari250gttdm”, “models/tdmcars/ferrari250gt.mdl” , 2000, nil, false )
AddCustomVehicle(“Lamborghini Gallardo”, “gallardotdm”, “models/tdmcars/gallardo.mdl” , 2000, nil, false )
AddCustomVehicle(“Ford GT500”, “gt500tdm”, “models/tdmcars/gt500.mdl” , 2000, nil, false )
AddCustomVehicle(“Land Rover”, “landrovertdm”, “models/tdmcars/landrover.mdl” , 2000, nil, false )
AddCustomVehicle(“VOlksWagon Golf MK2”, “golfmk2tdm”, “models/tdmcars/golf_mk2.mdl” , 2000, nil, false )
AddCustomVehicle(“Lamborghini Murcielago”, “murcielagotdm”, “models/tdmcars/murcielago.mdl” , 2000, nil, false )
AddCustomVehicle(“Dodge RAM”, “dodgeramtdm”, “models/tdmcars/dodgeram.mdl” , 2000, nil, false )
AddCustomVehicle(“Mazda RX8”, “rx8tdm”, “models/tdmcars/rx8.mdl” , 2000, nil, false )
AddCustomVehicle(“Spark”, “sparktdm”, “models/tdmcars/spark.mdl” , 2000, nil, false )
AddCustomVehicle(“Toyota Supra”, “supratdm”, “models/tdmcars/supra.mdl” , 2000, nil, false )
AddCustomVehicle(“Ford Transit”, “transittdm”, “models/tdmcars/transit.mdl” , 2000, nil, false )
AddCustomVehicle(“Lamborghini Reventon”, “reventonrtdm”, “models/tdmcars/lamborghini reventon.mdl” , 2000, nil, false )
AddCustomVehicle(“Cadillac Escalade”, “escaladetdm”, “models/tdmcars/escalade.mdl” , 2000, nil, false )
AddCustomVehicle(“507”, “507tdm”, “models/tdmcars/bmw507.mdl” , 2000, nil, false )[/lua]

Wrong place. This is meant to go in the Developer Discussion part.

Also I recommend this: http://forum.facepunch.com/showthread.php?t=1266627

It’s simple to use and it works because I use it. It’s extremely simple to add new cars and add VIP cars. It also saves, so if you have already bought the car you won’t need to again.

I thought it goes here since im looking for help.

[editline]7th September 2013[/editline]

Post it here: http://facepunch.com/forumdisplay.php?f=65

ok