• Think hook not able to use GetName()?
    3 replies, posted
[lua]function think() local all = ents.GetAll() for _,v in pairs(all) do if v:GetName() == "nectar_cap_red" then RedCap = v end if v:GetName() == "nectar_cap_blue" then BlueCap = v end end if RedCap:IsValid() and BlueCap:IsValid() then local minr, maxr = RedCap:WorldSpaceAABB() local minb, maxb = RedCap:WorldSpaceAABB() local in_r = ents.FindInBox(minr, maxr) local in_b = ents.FindInBox(minb, maxb) for k,v in pairs(in_r) do if v:GetClass() != "ants_nectar" then table.remove(in_r, k) k = k - 1 end end for k,v in pairs(in_b) do if v:GetClass() != "ants_nectar" then table.remove(in_b, k) k = k - 1 end end team.SetScore(1, #in_r) team.SetScore(2, #in_b) else print("no cap zones!") end end hook.Add("Think", "think", think)[/lua] It's erroring with v:GetName(). Saying it's a nil method. Why is that? I thought Think would be able to use serverside functions... [code] Hook 'think' Failed: ants\gamemode\shared.lua:301: attempt to call method 'GetName' (a nil value)[/code]
Are you trying to get the entity's class? You should try v:[URL="http://wiki.garrysmod.com/?title=Entity.GetClass"]GetClass()[/URL].
Don't put this is shared, it will try to run it on both the server and the client. And GetName doesn't exist on the client. Anyway, from the looks of it, it looks like you'd do this only serverside, so put it somewhere in init.lua. [editline]10:50AM[/editline] And give a unique name to your Think hook, I'm not sure "think" is unique.
Ok I can do that. Thanks! Also - no, I'm not trying to get the class. It's a trigger_multiple named nectar_cap_red and nectar_cap_blue that functions as the capture zone for nectar drops.
Sorry, you need to Log In to post a reply to this thread.