write a roblox script where theres a custom healthbar. then. the green healthbar would turn black if the player touched a part named: "soup". it damages every 0.5 seconds (stops at 3 seconds) before turning back to normal. basically it's a poisonous soup
Assistant
Here is a Roblox script that fulfills your requirements:
LocalScript in StarterGui
This script creates a custom health bar, disables the default health bar, updates the health bar based on the player's health, and changes the health bar color when the player is poisoned.
lua
-- LocalScript in StarterGui
-- Disable default health bar
local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)
-- Get the player
local player = game:GetService("Players").LocalPlayer
-- Wait for character
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local healthBarFrame
local healthBar
-- Create a RemoteEvent reference
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HealthBarEvent = ReplicatedStorage:WaitForChild("HealthBarEvent")
-- Function to create health bar
local function createHealthBar()
-- Create the custom health bar GUI
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "CustomHealthBar"
screenGui.Parent = player:WaitForChild("PlayerGui")
healthBarFrame = Instance.new("Frame")
healthBarFrame.Size = UDim2.new(0.3, 0, 0.05, 0)
healthBarFrame.Position = UDim2.new(0.35, 0, 0.9, 0)
healthBarFrame.BackgroundColor3 = Color3.new(0, 0, 0)
healthBarFrame.BorderSizePixel = 1
healthBarFrame.Parent = screenGui
-- The actual health bar
healthBar = Instance.new("Frame")
healthBar.Size = UDim2.new(1, 0, 1, 0)
healthBar.Position = UDim2.new(0, 0, 0, 0)
healthBar.BackgroundColor3 = Color3.new(0, 1, 0) -- Green
healthBar.BorderSizePixel = 0
healthBar.Parent = healthBarFrame
-- Update function
local function updateHealth()
if humanoid and humanoid.MaxHealth > 0 then
local healthPercent = humanoid.Health / humanoid.MaxHealth
healthBar.Size = UDim2.new(healthPercent, 0, 1, 0)
end
end
-- Connect health changed event
humanoid.HealthChanged:Connect(updateHealth)
-- Update the health bar initially
updateHealth()
end
-- Function to handle character added
local function onCharacterAdded(newCharacter)
character = newCharacter
humanoid = character:WaitForChild("Humanoid")
-- Remove old health bar GUI if any
local oldGui = player.PlayerGui:FindFirstChild("CustomHealthBar")
if oldGui then
oldGui:Destroy()
end
createHealthBar()
end
player.CharacterAdded:Connect(onCharacterAdded)
if character then
onCharacterAdded(character)
end
-- Handle the RemoteEvent to change health bar color
HealthBarEvent.OnClientEvent:Connect(function(action)
if action == "PoisonStart" then
if healthBar then
healthBar.BackgroundColor3 = Color3.new(0, 0, 0) -- Black
end
elseif action == "PoisonEnd" then
if healthBar then
healthBar.BackgroundColor3 = Color3.new(0, 1, 0) -- Green
end
end
end)
Script in ServerScriptService
This script ensures that the HealthBarEvent RemoteEvent exists in ReplicatedStorage.
lua
-- Script in ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HealthBarEvent = ReplicatedStorage:FindFirstChild("HealthBarEvent")
if not HealthBarEvent then
HealthBarEvent = Instance.new("RemoteEvent")
HealthBarEvent.Name = "HealthBarEvent"
HealthBarEvent.Parent = ReplicatedStorage
end
Script under the "soup" Part
This script handles the poisoning effect when a player touches the "soup" part. It damages the player every 0.5 seconds for 3 seconds and communicates with the client to change the health bar color.
lua
-- Script under the "soup" Part
local soup = script.Parent
local debounceTime = 0.5 -- Time between damage ticks
local totalDuration = 3 -- Total duration of the effect in seconds
local damagePerTick = 5 -- Amount of damage per tick
local touchingPlayers = {}
local function onTouch(otherPart)
local character = otherPart.Parent
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
local player = character and game.Players:GetPlayerFromCharacter(character)
if humanoid and player and not touchingPlayers[player] then
touchingPlayers[player] = true -- Debounce per player
-- Send RemoteEvent to client to change health bar to black
local HealthBarEvent = game.ReplicatedStorage:FindFirstChild("HealthBarEvent")
if HealthBarEvent then
HealthBarEvent:FireClient(player, "PoisonStart")
end
-- Damage the player every 0.5 seconds for 3 seconds
local ticks = totalDuration / debounceTime -- Calculate number of ticks
local tickCount = 0
while tickCount < ticks and humanoid.Health > 0 do
humanoid:TakeDamage(damagePerTick)
tickCount = tickCount + 1
wait(debounceTime)
end
-- After 3 seconds, send RemoteEvent to change health bar back to normal
if HealthBarEvent then
HealthBarEvent:FireClient(player, "PoisonEnd")
end
touchingPlayers[player] = nil -- Reset debounce for player
end
end
soup.Touched:Connect(onTouch)
Instructions to Set Up:
Create the "soup" Part:
Add a Part to your workspace and name it soup.
Place it where players can touch it.
Add the above Script as a child of this part.
Set Up the RemoteEvent:
Add the Script to ServerScriptService to ensure the HealthBarEvent RemoteEvent exists.
Set Up the LocalScript:
In StarterGui, add a LocalScript with the provided code.
This script creates the custom health bar and handles GUI updates.
Test the Game:
Play the game in Roblox Studio.
Observe that the default health bar is disabled.
The custom health bar appears at the bottom of the screen.
When the player touches the "soup" part, the health bar turns black.
The player's health decreases every 0.5 seconds for 3 seconds.
After 3 seconds, the health bar returns to green.
Notes:
The scripts handle player respawning. When a player dies and respawns, the custom health bar is recreated and connected to the new character.
If the player dies during the poison effect, the health bar will reset to green upon respawn.
Debouncing is used to prevent multiple poison effects from stacking if the player touches the "soup" multiple times rapidly.
Customization:
You can adjust the damagePerTick, debounceTime, and totalDuration variables in the "soup" part script to change the poison effect's severity and duration.
You can customize the appearance of the health bar by modifying the properties in the LocalScript.
Final Remarks:
This setup creates a custom health bar and simulates a poison effect when the player touches a specific part named "soup". The health bar visually reflects the player's health and changes color during the poison effect, enhancing the gameplay experience.