Best way to keep player from entering certain area (Vector:WithinAABox)
3 replies, posted
Hello.
I want to keep players out of certain areas which I've done with Vector:WithinAABox(), but I've read that the way I've done it is not an optimal way to do it because it'll have the server beg for mercy, which is understandable considering it'll run this every frame:
function checkPLoc()
local curmap = game.GetMap()
local groups = nil
for k, v in pairs (Locations[curmap].allowedGroups) do
groups = v
end
for _, ply in pairs(player.GetAll()) do
if (ply:GetPos():WithinAABox(Locations[curmap].minVec, Locations[curmap].maxVec) and ply:GetUserGroup() == groups) then
--ply:SetVelocity(Vector(0, -2000, 0))
ply:Kill()
end
end
end
hook.Add("Think", "checkPLoc", checkPLoc)
So since this way isn't an optimal way to do it, what is? Running it clientside? The script is currently in a shared lua file btw.
I'm fairly new to GLua, so I'll also happily take feedback/guidance on how to improve on the things I'm doing wrong.
Thanks in advance
You could probably make an entity and set its origin to the center of the area, then set its collision boundaries to the AABox of the area, and then use the entity's built-in StartTouch/Touch hooks to kill anyone who touches it if they meet some conditions.
If you want to show grace, you can adjust thier velocity as you first intended, though use Physobj:SetVelocityInsantenious()
I like that idea. I've played around a bit with an entity and it really is a better solution. However, I have multiple locations that all have different information stored in a table for me to easily add more locations if needed, so would it be possible to make it create a new entity for each location in the table and assign the respective functionality and properties of each location?
Alright thanks, I'll use this instead.
Sorry, you need to Log In to post a reply to this thread.