You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I am attempting to create a Health Bar GUI which decreases in size and changes color based on the percentage of health remaining ( 0 - 40 percent = red, 41- 75 percent = yellow, 76 - 100 percent = green) I also want my GUI’s text label to display the remaining health along with the maximum health value which is the player’s defense datastore2 value divided by 10000 with 100 as the minimum.
- What is the issue?
My script to tween the Health Bar GUI has an error and I cannot find a way to change the color based on the percentile value.
- What solutions have you tried so far?
I have created a Datastore2 defense value and attempted to create a script to tween my GUI, change color based on health out of 100 max health and get change the players max health to their defense Datastore2 value divided by 10000 + 100 as the base health.
Here is my current Datastore2 server script
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local DataStore2 = require(1936396537)
local defaultDefenseAmount = 100
players.PlayerAdded:Connect(function(player)
local defenseStore = DataStore2("defense", player)
local function updateClientDefense(amount)
replicatedStorage.RemoteEvents.UpdateClientCurrency:FireClient(player, amount)
end
updateClientDefense(defenseStore:Get(defaultDefenseAmount))
defenseStore:OnUpdate(UpdateClientDefense)
end)
And here is my attempted Health Bar script located in StarterPlayerScripts
local healthbar = game.Players.LocalPlayer.PlayerGui.Main.Stats.HealthBar
local healthText = game.Players.LocalPlayer.PlayerGui.Main.Stats.TextHealth
local human = script.Parent:WaitForChild("Humanoid")
local tween = game:GetService("TweenService")
local replicatedStorage = game:GetService("ReplicatedStorage")
replicatedStorage.RemoteEvents.UpdateClientCurrency.OnClientEvent:Connect(function(amount)
human.MaxHealth = amount / 10000 + 100
end
if human.Health < human.MaxHealth then
while wait(1) do
for k, v in pairs(players:GetChildren()) do
player.Health + 1
end
end
end
human.HealthChanged:Connect(function()
local percent = human.Health / human.MaxHealth
tween:Create(healthbar,TweenInfo.new(0.5),(Size = UDim2.new(percent,0,healthbar.Size.Y.Scale,0))):Play()
if human.Health >= 75 then
healthbar.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
elseif human.Health >= 50 and human.Health < 75 then
healthbar.BackgroundColor3 = Color3.fromRGB(255, 255, 0)
elseif human.Health >= 25 and human.Health < 50 then
healthbar.BackgroundColor3 = Color3.fromRGB(255, 128, 0)
else
healthbar.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
end
healthText.Text = math.floor(human.Health + 0.5) .."/".. math.floor(human.MaxHealth + 0.5)
end)