Trying to use this code to detect if a car is in a certain spawn location, and if there is, to try the next location. Doesn't seem to be working. I keep getting this error
Here is my table code:
SGMRP.vehicles.npc["Bus"] = {
title = "RTA",
model = "models/suits/male_0" .. math.random(1, 9) .. "_closed_tie.mdl",
pos = Vector(-1084.092529, 4282.931152, 608.031250),
ang = Angle(0, 90, 0),
teams = {
[TEAM_BUSDRIVER] = true
},
spawn = {
[1] = {
pos = Vector(-1450.528687, 4477.806152, 600.031250),
ang = Angle(0, 0, 0)
},
[2] = {
pos = Vector(-831.973022, 4006.948242, 600.031250),
ang = Angle(0, 90, 0)
}
}
}
Here is the code to check the spawn points
for k, v in pairs(SGMRP.vehicles.npc[npc:GetNPCKey()]) do
for kV, vV in pairs(ents.FindInSphere(v[tCount].pos, 1000)) do
if !vV:IsVehicle() then
car:SetAngles(v[tCount].ang)
car:SetPos(v[tCount].pos)
elseif vV:IsVehicle() then
print("car in area")
if v[tCount + 1] == nil then
print("No spanws left")
SGMRP.Notify(ply, "No more spawn locations. Try again in a bit", 1, 5)
else
print("before", tCount)
tCount = tCount + 1
print("after", tCount)
continue
end
end
end
end
Any ideas why I'm getting this error? Thanks
From the looks of that code, you're probably trying to do:
for k, v in pairs(SGMRP.vehicles.npc[npc:GetNPCKey()].spawn) do
rather than the first line in that snippet because in that code you're getting (title, model, pos, ...) as the 'k' and the corresponding value as v
I don't think I can do that because I want to check all spawn points, and when there are no free spawnpoints, to let the user know. How would I do it that way?
Ah I see, I tried to answer without actually trying to see what it is you were trying to do.
Anyway here's what I figure you want to do:
local found_spawn = false
for k, v in pairs(SGMRP.vehicles.npc[npc:GetNPCKey()].spawn) do --Iterate over every spawn location
local has_vehicle = false
for kV, vV in pairs(ents.FindInSphere(v.pos, 1000)) do --Find cars in the spawn
if vV:IsVehicle() then
has_vehicle = true --If you find one, set this to true to indicate it's used
break
end
end
if not has_vehicle then -- If the var wasn't set to true then the spawn is available
found_spawn = true --Set this to true to indicate you found a spawn
--Do your stuff here
break
end
end
if not found_spawn then
--There's no spawns left
end
Let me know if I understood wrong
Works. Thanks very much for the help!
Sorry, you need to Log In to post a reply to this thread.