Untitled

 avatar
unknown
plain_text
10 days ago
8.3 kB
2
Indexable
local UserInputService = game:GetService("UserInputService")
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local highlightEnabled = true
local aimEnabled = false
local aimAtHostile = false
local povRadius = 100

-- Function to make an object visible through walls
local function makeVisibleThroughWalls(object, isAmmo)
    -- Create a BillboardGui
    local billboardGui = Instance.new("BillboardGui")
    billboardGui.Size = UDim2.new(0, 100, 0, 100) -- Size of the BillboardGui
    billboardGui.StudsOffset = Vector3.new(0, 2, 0) -- Offset from the object
    billboardGui.AlwaysOnTop = true -- Make it always visible through walls
    billboardGui.Parent = object

    -- Create a TextLabel inside the BillboardGui
    local textLabel = Instance.new("TextLabel")
    textLabel.Size = UDim2.new(1, 0, 1, 0) -- Fill the entire BillboardGui
    textLabel.BackgroundTransparency = 1 -- Make the background transparent
    textLabel.Text = object.Name -- Display the name of the object
    textLabel.TextColor3 = isAmmo and Color3.new(0.5, 1, 0.5) or Color3.new(1, 0, 0) -- Light green for ammo, red for others
    textLabel.TextSize = 10 -- Reduce the text size
    textLabel.Parent = billboardGui
end

-- Function to highlight or remove highlight from an object
local function toggleHighlight(object, isAmmo, enable)
    if enable then
        makeVisibleThroughWalls(object, isAmmo)
    else
        local billboardGui = object:FindFirstChildOfClass("BillboardGui")
        if billboardGui then
            billboardGui:Destroy()
        end
    end
end

-- Function to highlight all active hostiles
local function highlightActiveHostiles(enable)
    local activeHostiles = Workspace:FindFirstChild("activeHostiles")
    if activeHostiles then
        -- Loop through each child in the activeHostiles folder
        for _, hostile in pairs(activeHostiles:GetChildren()) do
            toggleHighlight(hostile, false, enable)

            -- Check if the hostile is dead
            local humanoid = hostile:FindFirstChildOfClass("Humanoid")
            if humanoid then
                humanoid.HealthChanged:Connect(function(health)
                    if health <= 0 then
                        toggleHighlight(hostile, false, false)
                    end
                end)
            end
        end

        -- Listen for new objects added to the activeHostiles folder
        activeHostiles.ChildAdded:Connect(function(child)
            toggleHighlight(child, false, enable)

            -- Check if the new hostile is dead
            local humanoid = child:FindFirstChildOfClass("Humanoid")
            if humanoid then
                humanoid.HealthChanged:Connect(function(health)
                    if health <= 0 then
                        toggleHighlight(child, false, false)
                    end
                end)
            end
        end)
    else
        warn("activeHostiles folder not found in Workspace")
    end
end

-- Function to highlight all ammo
local function highlightAmmo(enable)
    local interactables = Workspace:FindFirstChild("Interactables")
    if interactables then
        for _, item in pairs(interactables:GetChildren()) do
            if item:IsA("Model") and item:FindFirstChild("Interact") and item.Name:find("Ammo") then
                toggleHighlight(item, true, enable)
            end
        end

        -- Listen for new objects added to the Interactables folder
        interactables.ChildAdded:Connect(function(child)
            if child:IsA("Model") and child:FindFirstChild("Interact") and child.Name:find("Ammo") then
                toggleHighlight(child, true, enable)
            end
        end)
    else
        warn("Interactables folder not found in Workspace")
    end
end

-- Function to find the nearest hostile within the player's FOV
local function findNearestHostileInFOV()
    local activeHostiles = Workspace:FindFirstChild("activeHostiles")
    if not activeHostiles then
        return nil
    end

    local nearestHostile = nil
    local shortestDistance = math.huge
    local camera = Workspace.CurrentCamera
    local screenCenter = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)

    for _, hostile in pairs(activeHostiles:GetChildren()) do
        if hostile:IsA("Model") and hostile:FindFirstChild("HumanoidRootPart") then
            local screenPoint = camera:WorldToViewportPoint(hostile.HumanoidRootPart.Position)
            local distanceFromCenter = (Vector2.new(screenPoint.X, screenPoint.Y) - screenCenter).Magnitude
            if distanceFromCenter < povRadius and distanceFromCenter < shortestDistance then
                -- Check if the hostile is within the player's FOV
                local directionToHostile = (hostile.HumanoidRootPart.Position - camera.CFrame.Position).Unit
                local cameraDirection = camera.CFrame.LookVector
                local dotProduct = cameraDirection:Dot(directionToHostile)
                local fovThreshold = math.cos(math.rad(45)) -- 45 degrees FOV

                if dotProduct > fovThreshold then
                    shortestDistance = distanceFromCenter
                    nearestHostile = hostile
                end
            end
        end
    end

    return nearestHostile
end

-- Function to aim at the nearest hostile's head within the player's FOV
local function aimAtNearestHostileInFOV()
    local nearestHostile = findNearestHostileInFOV()
    if nearestHostile and nearestHostile:FindFirstChild("Head") then
        local targetPosition = nearestHostile.Head.Position
        local direction = (targetPosition - Workspace.CurrentCamera.CFrame.Position).Unit
        Workspace.CurrentCamera.CFrame = CFrame.new(Workspace.CurrentCamera.CFrame.Position, Workspace.CurrentCamera.CFrame.Position + direction)
    end
end

-- Create a ScreenGui for the aim indicator
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "AimIndicatorGui"
screenGui.ResetOnSpawn = false -- Ensure the ScreenGui persists across respawns
screenGui.Parent = player:WaitForChild("PlayerGui")

-- Create a TextLabel for the aim indicator
local aimIndicator = Instance.new("TextLabel")
aimIndicator.Size = UDim2.new(0.2, 0, 0.05, 0)
aimIndicator.Position = UDim2.new(0.4, 0, 0.9, 0)
aimIndicator.BackgroundTransparency = 1
aimIndicator.TextScaled = true
aimIndicator.TextSize = 14
aimIndicator.TextColor3 = Color3.new(1, 1, 1)
aimIndicator.Parent = screenGui

-- Make the aim indicator draggable
aimIndicator.Active = true
aimIndicator.Draggable = true

-- Function to update the aim indicator text
local function updateAimIndicator()
    if aimEnabled then
        aimIndicator.Text = "Aim Enabled"
    else
        aimIndicator.Text = "Aim Disabled"
    end
end

-- Initial aim indicator update
updateAimIndicator()

-- Toggle highlight on key press
UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.V then
        highlightEnabled = not highlightEnabled
        highlightAmmo(highlightEnabled)
        highlightActiveHostiles(highlightEnabled)
    elseif input.KeyCode == Enum.KeyCode.Y then
        aimEnabled = not aimEnabled
        updateAimIndicator()
    elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
        aimAtHostile = true
    elseif input.KeyCode == Enum.KeyCode.Up then
        povRadius = povRadius + 10
    elseif input.KeyCode == Enum.KeyCode.Down then
        povRadius = povRadius - 10
    end
end)

UserInputService.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        aimAtHostile = false
    end
end)

-- Update aim at nearest hostile within the player's FOV when right mouse button is held
RunService.RenderStepped:Connect(function()
    if aimEnabled and aimAtHostile then
        aimAtNearestHostileInFOV()
    end
end)

-- Initial highlight state for ammo and active hostiles
highlightAmmo(highlightEnabled)
highlightActiveHostiles(highlightEnabled)
Leave a Comment