Sure! Let’s go over some Player Health Scripts. I’ll break down the different versions and their uses, starting from simple ones to more advanced health-related systems.
1. Basic Health Script
This is the simplest script where you can set a player’s health when they spawn and handle basic player death.
Steps to Add the Script:
1. Open Roblox Studio and load your game.
2. In the Explorer panel, right-click ServerScriptService.
3. Select Insert Object > Script to create a new script.
4. Delete the default code and paste the following script.
Basic Health Script:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
— Wait for the humanoid (the part that controls health) to exist
local humanoid = character:WaitForChild(“Humanoid”)
— Set the player’s health to 100 when they spawn
humanoid.Health = 100
— If the player’s health reaches zero, print a message
humanoid.Died:Connect(function()
print(player.Name .. ” has died!”)
end)
end)
end)
What it does:
• When a player joins, the script waits for their character to load.
• It finds the Humanoid part of the character, which controls the player’s health.
• The script sets the player’s health to 100 when they spawn.
• If the player’s health reaches 0, it prints a message in the output saying that the player has died.
2. Health with Health Bar Display
Now let’s make a script that shows a health bar in the user interface (UI) to track the player’s health.
Steps to Add the Script:
1. Follow the same steps as before to add a new script in ServerScriptService.
2. After adding the script, we will also need to create a ScreenGui for the health bar.
Health with Health Bar Script:
1. First, create a ScreenGui in StarterGui.
2. Inside the ScreenGui, create a Frame to act as the background for the health bar.
3. Inside the Frame, create a TextLabel or Frame that will represent the health bar’s fill.
Now paste this script into a LocalScript inside StarterPlayerScripts (since this runs on the player’s client).
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild(“Humanoid”)
— Create a GUI for the health bar
local playerGui = player:WaitForChild(“PlayerGui”)
local screenGui = Instance.new(“ScreenGui”)
screenGui.Parent = playerGui
local healthBarBackground = Instance.new(“Frame”)
healthBarBackground.Size = UDim2.new(0.2, 0, 0.05, 0) — 20% width and 5% height
healthBarBackground.Position = UDim2.new(0.4, 0, 0, 0)
healthBarBackground.BackgroundColor3 = Color3.fromRGB(255, 0, 0) — Red background
healthBarBackground.Parent = screenGui
local healthBar = Instance.new(“Frame”)
healthBar.Size = UDim2.new(1, 0, 1, 0) — Takes up all the space of the background
healthBar.BackgroundColor3 = Color3.fromRGB(0, 255, 0) — Green color
healthBar.Parent = healthBarBackground
— Update the health bar as the player’s health changes
humanoid.HealthChanged:Connect(function()
local healthPercentage = humanoid.Health / humanoid.MaxHealth
healthBar.Size = UDim2.new(healthPercentage, 0, 1, 0)
end)
— If the player dies, hide the health bar
humanoid.Died:Connect(function()
screenGui:Destroy()
end)
end)
end)
What it does:
• When the player spawns, it creates a ScreenGui with a Frame for the health bar background and another Frame that represents the health itself.
• The script tracks the player’s health and updates the health bar size proportionally based on the player’s current health.
• If the player dies, the health bar is removed.
3. Health with Regeneration
This version includes a feature where the player’s health regenerates over time.
Steps to Add the Script:
1. Insert the script into ServerScriptService.
2. This script will set the player’s initial health to 100 and regenerate 2 health points every second.
Health with Regeneration Script:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild(“Humanoid”)
— Set the player’s health to 100 when they spawn
humanoid.Health = 100
— Regenerate health every second
while humanoid.Health > 0 do
wait(1) — Wait for 1 second
if humanoid.Health < humanoid.MaxHealth then
humanoid.Health = humanoid.Health + 2 — Regenerate 2 health points
end
end
— If the player’s health reaches zero, print a message
humanoid.Died:Connect(function()
print(player.Name .. ” has died!”)
end)
end)
end)
What it does:
• When the player spawns, it sets their health to 100.
• The script regenerates 2 health points every second if the player’s health is below the maximum health.
• If the player dies (health reaches 0), a message will be printed in the output.
4. Player Health with Damage System
This version includes the ability to deal damage to the player when they touch a harmful object, like a trap or enemy.
Steps to Add the Script:
1. Insert a Script in ServerScriptService.
2. Create a Part in the game world that will deal damage to players (e.g., a trap or obstacle).
3. Name the part HarmfulPart or something that makes sense for your game.
Player Damage Script:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild(“Humanoid”)
— Set the player’s health to 100 when they spawn
humanoid.Health = 100
— Damage the player if they touch a harmful part
local harmfulPart = game.Workspace:WaitForChild(“HarmfulPart”) — The harmful part in the game
harmfulPart.Touched:Connect(function(hit)
local character = hit.Parent
if character:IsA(“Model”) and character:FindFirstChild(“Humanoid”) then
local hitHumanoid = character:FindFirstChild(“Humanoid”)
if hitHumanoid then
hitHumanoid:TakeDamage(10) — Deal 10 damage to the player
print(player.Name .. ” took damage!”)
end
end
end)
— If the player’s health reaches zero, print a message
humanoid.Died:Connect(function()
print(player.Name .. ” has died!”)
end)
end)
end)
What it does:
• When a player spawns, the script sets their health to 100.
• The script checks if the player touches a specific part (e.g., HarmfulPart). If they do, it will deal 10 damage to their health.
• If the player’s health reaches zero, it prints a death message.
Conclusion
These health scripts give you a range of options to manage player health in your game. You can choose which one works best for your needs, depending on whether you want simple health management, regeneration, damage-taking, or a health bar display. As you get more comfortable, you can combine and modify these scripts to add even more features to your game!