-- Variables
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
local camera = workspace.CurrentCamera
local runService = game:GetService("RunService")
-- Create the Toggle Button GUI
local screenGui = Instance.new("ScreenGui", game.CoreGui)
local toggleButton = Instance.new("TextButton", screenGui)
toggleButton.Size = UDim2.new(0, 35, 0, 35)
toggleButton.Position = UDim2.new(0, 10, 0, 10)
toggleButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
toggleButton.Text = "ESP"
toggleButton.TextColor3 = Color3.fromRGB(255, 255, 255)
toggleButton.TextScaled = true
-- Variables to track the ESP state
local espEnabled = false
local espObjects = {}
-- Function to create ESP for a player
local function createESP(player)
if player == localPlayer then return end
local billboard = Instance.new("BillboardGui")
billboard.Adornee = nil
billboard.Size = UDim2.new(0, 200, 0, 50)
billboard.StudsOffset = Vector3.new(0, 3, 0)
billboard.AlwaysOnTop = true
local textLabel = Instance.new("TextLabel", billboard)
textLabel.Size = UDim2.new(1, 0, 1, 0)
textLabel.BackgroundTransparency = 1
textLabel.TextColor3 = Color3.new(1, 0, 0)
textLabel.TextStrokeTransparency = 0.5
textLabel.TextScaled = true
textLabel.Font = Enum.Font.SourceSansBold
-- Update ESP dynamically
local function updateESP()
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local rootPart = character.HumanoidRootPart
local distance = (rootPart.Position -
localPlayer.Character.HumanoidRootPart.Position).Magnitude
billboard.Adornee = rootPart
textLabel.Text = string.format("%s (%s)\n%.1f studs",
player.DisplayName, player.Name, distance)
else
billboard.Adornee = nil
end
end
-- Add the BillboardGui to the player's character
local function onCharacterAdded(character)
local humanoidRootPart = character:WaitForChild("HumanoidRootPart", 5)
if humanoidRootPart then
billboard.Parent = humanoidRootPart
end
end
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
end
runService.RenderStepped:Connect(updateESP)
table.insert(espObjects, billboard)
end
-- Toggle ESP On/Off
toggleButton.MouseButton1Click:Connect(function()
espEnabled = not espEnabled
if espEnabled then
for _, player in pairs(players:GetPlayers()) do
createESP(player)
end
players.PlayerAdded:Connect(createESP)
toggleButton.BackgroundColor3 = Color3.fromRGB(0, 255, 0) -- Green when
enabled
else
for _, espObj in pairs(espObjects) do
espObj:Destroy()
end
espObjects = {} -- Clear the ESP objects
toggleButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Red when
disabled
end
end)
-- Add ESP for all players when the script starts if ESP is enabled
if espEnabled then
for _, player in pairs(players:GetPlayers()) do
createESP(player)
end
players.PlayerAdded:Connect(createESP)
end