Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local LocalPlayer = Players.LocalPlayer
- local MaxRange = 325 -- Set the maximum range for highlights
- local RunService = game:GetService("RunService") -- To track heartbeat updates
- -- Function to apply the highlight to a player (visible only to local player)
- local function ApplyHighlight(Player)
- -- Skip highlighting the local player
- if Player == LocalPlayer then return end
- -- Wait for the player's character to load
- local Character = Player.Character or Player.CharacterAdded:Wait()
- local Humanoid = Character:WaitForChild("Humanoid")
- -- Create a Highlight instance
- local HightLighter = Instance.new("Highlight")
- HightLighter.Parent = Character -- Parent to the character so it's attached
- -- Set the highlight's properties
- HightLighter.FillTransparency = 1 -- Make the body fill fully transparent
- HightLighter.OutlineTransparency = 0 -- Make the outline fully visible
- HightLighter.OutlineColor = Color3.fromRGB(255, 0, 0) -- Red outline
- -- Create a BillboardGui to show the health bar
- local BillboardGui = Instance.new("BillboardGui")
- BillboardGui.Parent = Character
- BillboardGui.Adornee = Character.Head -- Attach the BillboardGui to the player's head
- BillboardGui.Size = UDim2.new(0, 50, 0, 5) -- Set the size of the health bar
- BillboardGui.StudsOffset = Vector3.new(0, 3, 0) -- Position the health bar above the head
- -- Create a Frame to represent the health bar
- local HealthBar = Instance.new("Frame")
- HealthBar.Parent = BillboardGui
- HealthBar.Size = UDim2.new(1, 0, 1, 0) -- Full width of the BillboardGui
- HealthBar.BackgroundTransparency = 1 -- Make background transparent
- -- Create a health bar foreground (the actual health bar)
- local HealthBarForeground = Instance.new("Frame")
- HealthBarForeground.Parent = HealthBar
- HealthBarForeground.Size = UDim2.new(1, 0, 1, 0) -- Full size of the parent frame
- HealthBarForeground.BackgroundTransparency = 0 -- Make the health bar visible
- HealthBarForeground.BorderSizePixel = 0 -- Remove border
- -- Function to update the health bar based on the player's health
- local function UpdateHealthBar()
- local healthRatio = Humanoid.Health / Humanoid.MaxHealth
- -- Set health bar width based on health ratio
- HealthBarForeground.Size = UDim2.new(healthRatio, 0, 1, 0)
- -- Color the health bar based on health ratio
- if healthRatio > 0.66 then
- -- Green
- HealthBarForeground.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
- elseif healthRatio > 0.33 then
- -- Orange
- HealthBarForeground.BackgroundColor3 = Color3.fromRGB(255, 165, 0)
- else
- -- Red
- HealthBarForeground.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
- end
- end
- -- Update the health bar whenever the player's health changes
- Humanoid.HealthChanged:Connect(UpdateHealthBar)
- -- Initial health bar update
- UpdateHealthBar()
- -- Optional: Health monitor to remove the highlight when the player dies
- local function Disconnect()
- HightLighter:Remove()
- BillboardGui:Remove() -- Remove the health bar when the player dies
- end
- Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
- if Humanoid.Health <= 0 then
- Disconnect()
- end
- end)
- end
- -- Function to apply the highlight only for enemies (based on teams)
- local function ApplyEnemyHighlight()
- -- Loop through all players in the game
- for _, Player in next, Players:GetPlayers() do
- -- If the player is not the local player, check their team and distance
- if Player ~= LocalPlayer then
- -- Check if player is within the MaxRange distance
- local Character = Player.Character
- if Character and Character:FindFirstChild("HumanoidRootPart") then
- local Distance = (LocalPlayer.Character.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude
- if Distance <= MaxRange then -- Check if the target is within range
- -- If the local player is on a team and the enemy is on a different team
- if LocalPlayer.Team and Player.Team and LocalPlayer.Team ~= Player.Team then
- -- Apply the highlight if they're an enemy
- ApplyHighlight(Player)
- elseif not LocalPlayer.Team then
- -- If the local player is not on a team, highlight everyone except the local player
- ApplyHighlight(Player)
- end
- end
- end
- end
- end
- end
- -- Function to ensure new players are highlighted upon joining
- local function OnPlayerAdded(Player)
- -- When a new player joins, wait briefly to ensure their character is loaded, then apply the highlight
- wait(1) -- Wait for 1 second to ensure the character is fully loaded
- Player.CharacterAdded:Connect(function()
- ApplyEnemyHighlight()
- end)
- end
- -- Apply highlight to all players already in the game
- ApplyEnemyHighlight()
- -- Connect player-added event to ensure new players are handled correctly
- Players.PlayerAdded:Connect(OnPlayerAdded)
- -- Apply highlight when the player's character is added
- LocalPlayer.CharacterAdded:Connect(function()
- ApplyEnemyHighlight()
- end)
- -- Loop to constantly apply the highlight every 5 seconds using RunService
- RunService.Heartbeat:Connect(function()
- ApplyEnemyHighlight() -- Reapply the enemy highlights regularly
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement