Good night how can I the lua code to find that, and only that class of entity in a sphere? The vehicle class is "laat". The code that I posted won't work because it will select all entities and it will always execute the first part and the else part so it always spawns a new vehicle. How can I fix this?
[CODE]
if ents.FindInSphere( pos1, 5 ) then
for k,v in pairs(ents.FindInSphere( pos1, 5 )) do
if (v:GetClass() == "laat") then
MsgC(Color(52, 152, 219), "Test")
DarkRP.notify(ply, 1, 5, "There is an aircraft occupying the spawn, move it")
-- If a vehicle exists I want it to not spawn a new one, but even when one exists it always executes the else statement because there are other entities close by and the code is selecting all of them
else
local laat = ents.Create("laat")
laat:SetModel("models/ish/starwars/laat/laat_mk2.mdl")
laat:SetPos(pos1)
laat:SetAngles(angle1)
laat:Spawn()
laat:Activate()
DarkRP.notify(ply, 1, 5, "You've spawned a ".. AircraftShop_Items[item].Name)
end
end
end
[/CODE]
Thank you to who helps me :)
The actual spawn code shouldn't be inside the loop. You can define a local variable first, set it to true if you find a laat entity in the table, and then check it afterward.
[lua]
local found
for k, v in pairs(ents.FindInSphere(pos1, 5)) do
if v:GetClass() == "laat" then
found = true
end
end
if found then
-- send error message
else
-- spawn entity
end
[/lua]
Also ents.FindInSphere() always returns a table, so the first if statement is unnecessary.
[QUOTE=Luni;52404977]The actual spawn code shouldn't be inside the loop. You can define a local variable first, set it to true if you find a laat entity in the table, and then check it afterward.
[lua]
local found
for k, v in pairs(ents.FindInSphere(pos1, 5)) do
if v:GetClass() == "laat" then
found = true
end
end
if found then
-- send error message
else
-- spawn entity
end
[/lua]
Also ents.FindInSphere() always returns a table, so the first if statement is unnecessary.[/QUOTE]
Thanks man I will try it, I actually thought of doing something like that but it was late last night and I couldnt think of how I would do it :P
Sorry, you need to Log In to post a reply to this thread.