• Code not working in a server?
    3 replies, posted
I've been trying to make a "Safe Zone" entity, but when I spawn more than 1 of it in a Local Server other ones don't work but the first one works. It's fine in single player though, and I'm sorry if my problem is really easy, I'm pretty new to LUA. Here's my code: local function SafeZone(victim, attacker) for m, k in pairs(ents.GetAll()) do if k:GetClass() == "crp_safe_zone" then local entities = ents.FindInBox(k.point1:GetPos(), k.point2:GetPos()) for i, v in pairs(player.GetAll()) do if table.HasValue(entities, v) then return false else end end end end end hook.Add("PlayerShouldTakeDamage", "SafeZone", SafeZone)
not too sure about the problem right now, but something else: if table.HasValue(entities, v) then return false else this is useless. You dont need to do a else and then dont do anything. just make if, and thats it. Means it will do IF condition is true then, if not, the if just wont execute, a else with no code is useless
You could do it easier: local function SafeZone(victim, attacker)     for m, k in pairs(ents.GetAll()) do         if k:GetClass() == "crp_safe_zone" then             if victim:GetPos():WithinAABox( k.point1:GetPos(), k.point2:GetPos() ) then                 return false             elseif attacker:GetPos():WithinAABox( k.point1:GetPos(), k.point2:GetPos() ) then                 attacker:ChatPrint("Do not attack players in a savezone!")                 return false             end         end     end end hook.Add("PlayerShouldTakeDamage", "SafeZone", SafeZone) I don't actually understand why you are doing it with entities. I would save pos1 and pos2 at a txt file by marking them with a toolgun etc.
For some reason when I'm using WithinAABox it doesn't detect if the player is in the box that I've made but when I used ents.FindInBox it works just fine. Also, I don't get why I need to save the positions in a text file. Thanks for the tip, I'll keep that in mind. k.point1 and k.point2 are 2 different entities, I'm basically making a customizable safe zone wherein you can just move 2 other entities to make a box. I'm sorry if I'm bothering you guys..
Sorry, you need to Log In to post a reply to this thread.