• Error I don't quite understand
    13 replies, posted
[ERROR] addons/ctf/lua/entities/capturepoint/init.lua:15: attempt to call method 'GetPos' (a nil value) 1. unknown - addons/ctf/lua/entities/capturepoint/init.lua:15 That's the error, I'm new to entites, so I don't understand what's going on, Here's my code located at init.lua line 15: local playersFound = ents.FindInSphere( ENT:GetPos(), 256 ) for k,v in ipairs(playersFound) do if IsValid(v) && v:IsPlayer() then print("I'm a giraffe") end end My goal is to print "I'm a giraffe" in console when a player gets near this entity.
You need to define the entities you want to get their Position. local ent = ents.GetAll() // get all entities on the server local playersFound = ents.FindInSphere( ent:GetPos(), 256 ) // get all entites in sphere for k , v in pairs(playerFound) do if IsValid(v) and v:IsPlayer() then print("I'm a giraffe") end end this code should work. Enjoy !
Will that work? I need ENT:GetPos() to be the entity that I'm creatings position, here's the full init.lua so you understand include("shared.lua") AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") function ENT:Initialize() self:SetModel('models/props_wasteland/controlroom_filecabinet002a.mdl') self:SetSolid(SOLID_BBOX) self:SetUseType(SIMPLE_USE) self:DropToFloor() end local playersFound = ents.FindInSphere( ENT:GetPos(), 256 ) for k,v in ipairs(playersFound) do if IsValid(v) && v:IsPlayer() then print("I'm a giraffe") end end I'm trying to create an entity, and then get that entity's position
Assuming you're using this in a method of your entity, use self, not ENT. ENT is just the global definition of the entity for when the file loads. This'll error, you're calling Entity/GetPos on an array of entities.
Also just tried the code you sent me, returned the exact same error :/
oh sry my bad try this one local GetEnts = ents.GetAll() -- get all entities on the server for k , v in pairs(GetEnts) do ent = v end local playersFound = ents.FindInSphere( ent:GetPos(), 256 ) -- get all entites in sphere for k , v in pairs(playerFound) do if IsValid(v) and v:IsPlayer() then print("I'm a giraffe") end end
You're not using this code in a method then. You have to have a way to call this code otherwise it's only going to run once. Simple solution is to use ENTITY/Think. This isn't what OP wants, this is basically just picking a random location and finding players in a sphere around it.
No, it isn't being ran in a method, Does it have to be to reference the entity? Again, here is the entirety of init.lua (reverted) include("shared.lua") AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") function ENT:Initialize() self:SetModel('models/props_wasteland/controlroom_filecabinet002a.mdl') self:SetSolid(SOLID_BBOX) self:SetUseType(SIMPLE_USE) self:DropToFloor() end local playersFound = ents.FindInSphere( ENT:GetPos(), 256 ) timer.Create("",number delay,number repetitions,function func) for k,v in ipairs(playersFound) do if IsValid(v) then print("I'm a giraffe") end end
Yes, code ran inside your entity isn't special in any way. `ENT` is provided as a global for you to setup the methods your entity will have, that's it. `ENT` isn't an entity. To reference an entity you have to define a method that'll be called somehow, e.g. in the ENTITY/Think method.
Okay, I'll give it a try
function ENT:Think() local playersFound = ents.FindInSphere( self:GetPos(), 256 ) for k,v in ipairs(playersFound) do if IsValid(v) && v:IsPlayer() then print("I'm a giraffe") end end end
Now its just saying i'm a giraffe constantly
If you're using the code @Bluzer provided then yeah, that's what it does.
Wait just added a player check, now its working, thanks mat!
Sorry, you need to Log In to post a reply to this thread.