I am trying to make a healthbar, but whenever my heath changes, the healthbar doesn’t shrink, I’ve tried something else but that didn’t work. I’ve run out of ideas and I need help. This is my script so far:
math.clamp() is useful for keeping a number in between a minimum and a maximum, an alternative to try besides 0.713*game.Players.LocalPlayer.Character.Humanoid.Health/100
-- Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
-- Player
local Player = Players.LocalPlayer
-- Character / Humanoid
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
-- Connection
Humanoid.HealthChanged:Connect(function()
local Health = math.clamp((0.713 * Humanoid.Health)/100, 0, 1)
TweenService:Create(script.Parent, Tweeninfo.new(0.5, Enum.EasingStyle.Linear), {Size = UDim2.fromScale(0.65, Health)}):Play()
-- If you don't want a tween you can remove the 2 lines above and replace it with the bottom line
--script.Parent.Size = UDim2.fromScale(0.65, math.clamp(0.713*Humanoid.Health/100, 0, 1))
end)
-- Service
local Players = game:GetService("Players")
-- Player
local Player = Players.LocalPlayer
-- Character / Humanoid
local Character = Player.Character or Player.CharactedAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")
-- Connection
Humanoid.HealthChanged:Connect(function()
script.Parent.Size = UDim2.fromScale(0.65, math.clamp(0.713*Humanoid.Health/100, 0, 1))
end)
Could you explain this script to me? I’d like to use stuff like this in the future.
Are you not very familiar with how local scripts work? Your script above is basically set up to do the same thing except TheDCrafts just uses the local player which makes it work.
It’s basically your script but a bit cleaned up and it’s not checking for a player added since it’s a local script and a player should exist when the local script is running.