From venuslock, 1 Month ago, written in Plain Text.
Embed
  1. -- zombie attack
  2. local EnemiesFolder = workspace.enemies
  3. local Players = game:GetService("Players")
  4. local Player = Players.LocalPlayer
  5. local Mouse = Player:GetMouse()
  6. local RunService = game:GetService("RunService")
  7. local TweenService = game:GetService("TweenService")
  8. local HitTI = TweenInfo.new(.4, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
  9.  
  10. local GetClosestToCursor = function()
  11.    local closestDistance = math.huge
  12.    local closestEnemy = nil
  13.  
  14.    for _, enemy in pairs(EnemiesFolder:GetChildren()) do
  15.        if not enemy:FindFirstChild("Head") then continue end
  16.        if not enemy:FindFirstChildOfClass("Humanoid") then continue end
  17.        if enemy.Humanoid.Health <= 0 then continue end
  18.  
  19.        local screenPos, visible = workspace.CurrentCamera:WorldToViewportPoint(enemy.Head.Position)
  20.  
  21.        local distance = (Vector2.new(Mouse.X, Mouse.Y) - Vector2.new(screenPos.X, screenPos.Y)).Magnitude
  22.        if distance < closestDistance then
  23.            closestEnemy = enemy
  24.            closestDistance = distance
  25.        end
  26.    end
  27.  
  28.    return closestEnemy
  29. end
  30.  
  31. local ClosestEnemy = GetClosestToCursor()
  32. RunService.Stepped:Connect(function(time, deltaTime)
  33.    ClosestEnemy = GetClosestToCursor()
  34. end)
  35.  
  36. local Highlight = function(ClosestEnemy)
  37.    local hl = Instance.new("Highlight")
  38.    hl.Adornee = ClosestEnemy
  39.    hl.FillColor = Color3.fromRGB(255, 0, 0)
  40.    hl.FillTransparency = .2
  41.    hl.OutlineColor = Color3.fromRGB(200, 0, 0)
  42.    hl.OutlineTransparency = .2
  43.    hl.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
  44.    hl.Parent = ClosestEnemy
  45.    local t = TweenService:Create(hl, HitTI, {FillTransparency = 1, OutlineTransparency= .8})
  46.    t.Completed:Connect(function()
  47.        hl:Destroy()
  48.    end)
  49.    t:Play()
  50. end
  51.  
  52.  
  53. local old; old = hookmetamethod(game, '__namecall', function(this, ...)
  54.   local args = {...}
  55.   local method = getnamecallmethod()
  56.  
  57.   if not checkcaller() and method == 'FireServer' and this.Name == "Gun" then
  58.        if ClosestEnemy then
  59.            task.spawn(Highlight, ClosestEnemy)
  60.            args[1]["Hit"] = ClosestEnemy.Head
  61.        end
  62.    end
  63.  
  64.   return old(this, unpack(args))
  65. end)