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” )

=“keyword operator”>=“storage function”>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 =“keyword operator”>=“keyword operator”>=“keyword operator”>= false
for k,v in pairs(NPCSHOP.UserGroups) do
if ply:GetUserGroup() == v then
isvip =“keyword operator”>= true
break
end
end
return isvip
end

//Utilizes darkrp’s notify
local function Notify(ply, text)
if not IsValid(ply) then return end
=“keyword operator”>=“keyword operator”>=“support function”>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]

config.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/eli.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”, 500000, nil, false)
NPCSHOP.AddVehicle(“350z”, “350ztdm”, “models/tdmcars/350z.mdl”, 65000, nil, false)
NPCSHOP.AddVehicle(“Scirocco”, “sciroccotdm”, “models/tdmcars/scirocco.mdl” , 10000, nil, false )
NPCSHOP.AddVehicle(“mercedes sl65 AMG”, “sl65amgtdm”, “models/tdmcars/sl65amg.mdl” , 125000, nil, false )
NPCSHOP.AddVehicle(“DodgeChargersrt8”, “chargersrt8tdm”, “models/tdmcars/chargersrt8.mdl” , 45000, nil, false )
NPCSHOP.AddVehicle(“MitsubishiEVOx”, “mitsu_evoxtdm”, “models/tdmcars/mitsu_evox.mdl” , 80000, nil, false )
NPCSHOP.AddVehicle(“FordFocus”, “focusrstdm”, “models/tdmcars/focusrs.mdl” , 15000, nil, false )
NPCSHOP.AddVehicle(“AudiTT”, “auditttdm”, “models/tdmcars/auditt.mdl” , 45000, nil, false )
NPCSHOP.AddVehicle(“DBS”, “dbstdm”, “models/tdmcars/dbs.mdl” , 750000, nil, false )
NPCSHOP.AddVehicle(“GMCVan”, “gmcvantdm”, “models/tdmcars/gmcvan.mdl” , 13000, nil, false )
NPCSHOP.AddVehicle(“BMWM5E60”, “bmwm5e60tdm”, “models/tdmcars/bmwm5e60.mdl” , 70000, nil, false )
NPCSHOP.AddVehicle(“c4”, “c4tdm”, “models/tdmcars/cit_c4.mdl” , 2000, nil, false )
NPCSHOP.AddVehicle(“c12”, “c12tdm”, “models/tdmcars/zondac12.mdl” , 2000, nil, false )
NPCSHOP.AddVehicle(“ceed”, “ceedtdm”, “models/tdmcars/kia_ceed.mdl” , 5000, nil, false )
NPCSHOP.AddVehicle(“Ferrari250”, “ferrari250gttdm”, “models/tdmcars/ferrari250gt.mdl” , 400000, nil, false)
NPCSHOP.AddVehicle(“Lamborghini Gallardo”, “gallardotdm”, “models/tdmcars/gallardo.mdl” , 350000, nil, false )
NPCSHOP.AddVehicle(“FordGT500”, “gt500tdm”, “models/tdmcars/gt500.mdl” , 90000, nil, false )
NPCSHOP.AddVehicle(“LandRover”, “landrovertdm”, “models/tdmcars/landrover.mdl” , 30000, nil, false )
NPCSHOP.AddVehicle(“VOlksWagonGolfMK2”, “golfmk2tdm”, “models/tdmcars/golf_mk2.mdl” , 5000, nil, false )
NPCSHOP.AddVehicle(“LamborghiniMurcielago”, “murcielagotdm”, “models/tdmcars/murcielago.mdl” , 600000, nil, false )
NPCSHOP.AddVehicle(“DodgeRAM”, “dodgeramtdm”, “models/tdmcars/dodgeram.mdl” , 35000, nil, false )
NPCSHOP.AddVehicle(“Spark”, “sparktdm”, “models/tdmcars/spark.mdl” , 5000, nil, false )
NPCSHOP.AddVehicle(“ToyotaSupra”, “supratdm”, “models/tdmcars/supra.mdl” , 25000, nil, false )
NPCSHOP.AddVehicle(“507”, “507tdm”, “models/tdmcars/bmw507.mdl” , 70000, nil, false )[/lua]

[editline]6th September 2013[/editline]

Anyone know how to do this?

Well, I noticed your job restriction bug. If the ENUMs aren’t working from DarkRP, you’re loading your code earlier than they define them. Put your code to load in a hook / change the load order so the team enums load first.

As for the owner issue:

You called car.Owner = ply, but what is this? “car:own(ply)”

The error the console is outputting tells us that this :own function doesn’t exist. Try commenting out that line and seeing if it works.

In your spawn function somewhere ( before you spawn the vehicle ), you are going to need to go through the spawned vehicles and see if a car exists with .Owner set to the player. If so, deny spawning it.

Im kind of confused should i change car.owner = ply to car:own(ply)?

No; the error is saying that the function own, does not exist. I said remove that line for now, and when you want to spawn another car ( so around the top of the spawn car function ) you need to add something that goes through the spawned vehicles prop_vehicle_* and see if the person who wants to spawn a vehicle already has a vehicle out.

at line 129 instead of car:own try car:KeyOwn or car:KeyOwns or car:Own.

I tried all three and it still spawn unowned.

Does anyone know how to at least make it so 1 person can only spawn 1 car at a time?

open the server.cfg for this addon and change the car:own(ply) to car:keysOwn(ply) . Also change all Addmoney to addmoney , CanAfford to canAfford

:wink:

Member since 2008 and 2 posts? O.o rare occasion

A post was submitted in the facepunch thread for this with a possible fix.