Your first script
This walk-through builds a small script that tells you about your own character. Join any Roblox game first, so there is a character to read.
1. Make the file
Open the menu, go to Scripts, press +, type hello and press Create. evasive saves hello.lua and opens it in the editor:
-- hello.lua
print("Hello from hello.lua")Press F5. The Output card on the right shows three lines: "started", your "Hello from hello.lua" and "finished". The script ran its code and had nothing left to do, so it ended.
2. Read the game
Replace the code with this and press F5 again:
local Players = game:GetService("Players")
local me = Players.LocalPlayer
print("You are", me.DisplayName, "with user id", me.UserId)
print("Place id:", game.PlaceId)
print("Players in this server:", #Players:GetPlayers())game is the Roblox game you are in. GetService finds one of its services by class, and LocalPlayer is you. Everything you read comes straight from the running game, so the numbers are live.
3. Find your character
Your character is a Model that holds the body parts and a Humanoid. It can be missing for a moment (while you respawn, for example), so check before you use it:
local Players = game:GetService("Players")
local me = Players.LocalPlayer
local character = me.Character
if not character then
warn("No character right now")
return
end
local root = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChildOfClass("Humanoid")
if root and humanoid then
print("Standing at", root.Position)
print(string.format("Health %.0f of %.0f", humanoid.Health, humanoid.MaxHealth))
endFindFirstChild returns nil when there is no child of that name, instead of raising an error. That makes it the safe way to look things up.
4. Keep it running
A script that loops keeps running until you stop it. task.wait pauses the loop without freezing anything else:
local Players = game:GetService("Players")
local me = Players.LocalPlayer
while true do
local humanoid = me.Character and me.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
print(string.format("Health: %.0f", humanoid.Health))
end
task.wait(2)
endThe row in the list now shows "running". Take some damage in game and watch the number change. Press the stop button (or Shift+F5 in the editor) when you are done.
Every loop needs a wait in it. A loop that never waits would lock up the script engine, so evasive stops any script that runs for three seconds without yielding. How scripts run explains the details.
5. Show it on screen
Printing is fine for testing. Drawing is nicer to look at. This version puts your health in the top left corner of the Roblox window:
local Players = game:GetService("Players")
local me = Players.LocalPlayer
local label = Drawing.new("Text")
label.Position = Vector2.new(20, 80)
label.FontSize = 18
label.Outline = true
label.Color = Color3.fromRGB(120, 255, 140)
label.Visible = true
while true do
local humanoid = me.Character and me.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
label.Text = string.format("HP %.0f / %.0f", humanoid.Health, humanoid.MaxHealth)
else
label.Text = "No character"
end
task.wait(0.1)
endNew drawings start hidden, which is why the script sets Visible = true. When you stop the script, the text goes away by itself.
Where to go next
- How scripts run covers waiting, events and what happens when something goes wrong.
- Drawing on screen turns this into a box ESP.
- Reading the game shows how to find players, parts and anything else in the game.