Advertisement
Zaktak

Entity Spawning

Apr 23rd, 2020
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. ZKEntSpawnF4 = ZKEntSpawnF4 or {}
  2. ZKEntSpawnF4.config = ZKEntSpawnF4.config or {}
  3.  
  4. ZKEntSpawnF4.config.EntityOwners = {}
  5. ZKEntSpawnF4.config.Entities = {}
  6. ZKEntSpawnF4.config.OwnerIndex = 1
  7.  
  8.  
  9. function GetPlyEyeTracer(ply)
  10.     local pos = ply:GetEyeTrace().HitPos -- Vector of Where the Player is Looking.
  11.     if (pos == nil) then return nil end
  12.  
  13.     local Distance = (ply:GetPos()):Distance(pos) -- Get the Distance from player's position to tracer position.
  14.  
  15.  
  16.     -- If looking too far, return function --> unable to spawn
  17.     if (Distance > 500) then
  18.         ServerLog("[ZES]: Error, " .. ply:Nick() .. " Has tried to Spawn too far.".."\n")
  19.         return nil
  20.     end
  21.  
  22.     -- If looking too close, return function --> unable to spawn (Due to Spawn Kill)
  23.     if (Distance < 120) then
  24.         ServerLog("[ZES]: Error, " .. ply:Nick() .. " Has tried to Spawn too close.".."\n")
  25.         return nil
  26.     end
  27.  
  28.     return pos
  29. end
  30.  
  31.  
  32.  
  33. --[[--------------------
  34.           Test
  35. ----------------------]]
  36.  
  37.  
  38. DarkRP.createEntity("Test", {
  39.     ent = "Entity",
  40.     model = "model.mdl",
  41.     price = 0,
  42.     max = 1,
  43.     cmd = "cmds",
  44.     category = "Entities",
  45.     allowed = {TEAM_TEST},
  46.     customCheck = function(ply) return
  47.         table.HasValue({TEAM_TEST}, ply:Team())
  48.     end,
  49.     CustomCheckFailMsg = "You must be a TEST.",
  50.     spawn = function(ply, tr, tblEnt)
  51.         local curEnt = ents.Create("Entity")
  52.         curEnt:SetModel("model.mdl")
  53.         local posPly = GetPlyEyeTracer(ply)
  54.         local safeRange = Vector(200,200,70)
  55.         if (posPly == nil) then
  56.             posPly = ply:GetPos() + safeRange
  57.         end
  58.         curEnt:SetPos(posPly + Vector(0,0,50))
  59.         curEnt:Spawn()
  60.         curEnt:SetOwner(ply)
  61.         ZKEntSpawnF4.config.EntityOwners[ZKEntSpawnF4.config.OwnerIndex] = curEnt:GetOwner() -- Log the Owner
  62.         ZKEntSpawnF4.config.Entities[ZKEntSpawnF4.config.OwnerIndex] = curEnt -- Log the Entity with same Index
  63.         ZKEntSpawnF4.config.OwnerIndex = ZKEntSpawnF4.config.OwnerIndex + 1
  64.         return curEnt
  65.     end,
  66. })
  67.  
  68.  
  69. hook.Add("PlayerDeath", "ZK_OnPlayerDeath", function(vic, inf, at) -- Remove Entity on Death
  70.     if (table.HasValue(ZKEntSpawnF4.config.EntityOwners, vic)) then -- If the player that died is an Owner of an Entity
  71.         for k, v in pairs(ZKEntSpawnF4.config.EntityOwners) do
  72.             if (v == vic) then -- Check for more entities this player has
  73.                 local zEnt = ZKEntSpawnF4.config.Entities[k] -- Get the entity with the same index.
  74.                 if (IsValid(zEnt)) then -- If the entity is Valid
  75.                     zEnt:Remove() -- Remove the entity
  76.                 end
  77.             end
  78.         end
  79.     end
  80. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement