How can I simplify this code?

--[[
    Hi, How can I simplify this code that I have here?
    Im not sure if some of it is nessacary.
    
    Do I even need to do it?
--]]

-- HpVal     = IntValue
-- MaxHealth = Humanoid.MaxHealth

---------------------------------------------------------------------------
-- CODE:
---------------------------------------------------------------------------
local info = TweenInfo.new(.2) -- TweenInfo

local full  = Color3.new(0,1,0) -- Green
local empty = Color3.new(1,0,0) -- Red

HpVal.Changed:Connect(function(val)
    TextLabel.Text = val.."%" -- Updates TextLabel
end)

Humanoid.HealthChanged:Connect(function(Hp) -- When Health Changes
    local LerpedColor = empty:Lerp(full, Hp/MaxHealth) -- Color from Interpolation
    local HealthUDim = UDim2.fromScale(1, Hp/MaxHealth) -- Scale
    
    TweenService:Create(Frame, info, {BackgroundColor3 = LerpedColor}):Play() -- Tweens Color
    TweenService:Create(Frame, info, {Size = HealthUDim}):Play() -- Tweens Size
    TweenService:Create(HpVal, info, {Value = Hp}):Play() -- Tweens IntValue
end)



The script can be shortened by combining the three TweenService:Create() calls into a single call that updates all three properties at once.

local full  = Color3.new(0,1,0) -- Green
local empty = Color3.new(1,0,0) -- Red

HpVal.Changed:Connect(function(val)
    TextLabel.Text = val.."%" -- Updates TextLabel
end)

Humanoid.HealthChanged:Connect(function(Hp) -- When Health Changes
    local LerpedColor = empty:Lerp(full, Hp/MaxHealth) -- Color from Interpolation
    local HealthUDim = UDim2.fromScale(1, Hp/MaxHealth) -- Scale
    TweenService:Create(Frame, info, {
        BackgroundColor3 = LerpedColor,
        Size = HealthUDim,
        HpVal = Hp,
    }):Play() -- Tweens Color, Size, and IntValue
end)

HpVal isnt a property of a Frame?

Use this function.

function PlayTween(Object, Info, Property, ...)
	game:GetService("TweenService"):Create(Object, Info, {Property = ...}):Play()
end

Object, Info, property, Ordered List

Doesn’t exactly seem nessacary, It would be the exact same thing as TweenService:Create()

Also, I’m pretty sure you can just do this:

function Tween(Object, Info, ...)
	game:GetService("TweenService"):Create(Object, Info, ...):Play()
end

Sorry about that, Maybe try this one.

local full = Color3.new(0, 1, 0) -- Green
local empty = Color3.new(1, 0, 0) -- Red

HpVal.Changed:Connect(function(val)
    TextLabel.Text = val.."%" -- Updates TextLabel
end)

Humanoid.HealthChanged:Connect(function(Hp) -- When Health Changes
    local LerpedColor = empty:Lerp(full, Hp / MaxHealth) -- Color from Interpolation
    local HealthUDim = UDim2.fromScale(1, Hp / MaxHealth) -- Scale
    TweenService:Create(Frame, info, {
        BackgroundColor3 = LerpedColor,
        Size = HealthUDim
    }):Play() -- Tweens Color and Size
    TweenService:Create(HpVal, info, { Value = Hp }):Play() -- Tweens Health value
end)
1 Like

Yeah you could use this snippet, it works for me.

local IntValue = Instance.new("IntValue")
IntValue.Value = 10
IntValue.Parent = script
function PlayTween(Object, Info, ...)
	game:GetService("TweenService"):Create(Object, Info, ...):Play()
end
PlayTween(IntValue, TweenInfo.new(1), {Value = 0})

Also I’m sure tables would work for this.

2 Likes

Honestly, I think this code is pretty well made. I have no complaints about it. It’s a pretty short code so it’s hard to simply it even more. If you really want to make it a bit shorter, you can remove those 2 local variables in HealthChanged and put the values directly in the tween like so:

local info = TweenInfo.new(.2) -- TweenInfo

local full  = Color3.new(0,1,0) -- Green
local empty = Color3.new(1,0,0) -- Red

HpVal.Changed:Connect(function(val)
    TextLabel.Text = val.."%" -- Updates TextLabel
end)

Humanoid.HealthChanged:Connect(function(Hp) -- When Health Changes
    TweenService:Create(Frame, info, {BackgroundColor3 = empty:Lerp(full, Hp/MaxHealth)}):Play() -- Tweens Color
    TweenService:Create(Frame, info, {Size = UDim2.fromScale(1, Hp/MaxHealth)}):Play() -- Tweens Size
    TweenService:Create(HpVal, info, {Value = Hp}):Play() -- Tweens IntValue
end)

You shouldn’t stress to much about short code like this. It’s honestly not a big deal.

2 Likes

Your initial code looks clean enough as-is. Any sort of recombination just makes it less readable.

Not everything needs to be simplified to the max. I’d say it is simple enough.

You could merge the second tween with the first, but you don’t have to. You could also make use of the new string interpolation and make the val concatenation into

`{val}%`

But that’s it. Nothing else I would change personally.

1 Like

Just asking if it could be simplified as some code there seems unnessacary

It can be simplified, but the thing is that it doesn’t need to be simplified. In my opinion, this code is fine. Although, you could merge the first 2 tweens’s properties together to make one tween if you really want some simplification:

local info = TweenInfo.new(.2) -- TweenInfo

local full  = Color3.new(0,1,0) -- Green
local empty = Color3.new(1,0,0) -- Red

HpVal.Changed:Connect(function(val)
    TextLabel.Text = val.."%" -- Updates TextLabel
end)

HealthChanged:Connect(function(Hp) -- When Health Changes
    TweenService:Create(Frame, info, {BackgroundColor3 = empty:Lerp(full, Hp/MaxHealth), Size = UDim2.fromScale(1, Hp/MaxHealth)}):Play() -- Tweens Color & Size
    TweenService:Create(HpVal, info, {Value = Hp}):Play() -- Tweens IntValue
end)

Ok, Last thing, how exactly does that work, or how exactly do I do that?

Edit: nevermind, I fiqured it out.

It’s like a regular string but code placed between curly braces are treated as expressions. Example:

local x = 123

print(`Text here >{x}<  numbers: {62*22/55}. Other strings: {"a".."b"}`)

I haven’t tested this out but I assume it would work. Try to put that in a script and run it to see what it does.

1 Like

ah, that makes sense, I always wondered what backticks did, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.