Question about Tweening

Hello, recently i’ve been trying to achieve a “Low health” screen effect were the screen turns blurry depending on the percentage of your health, however the only way that i can seem to achieve this, is to do this:

local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")

local character = script.Parent
local humanoid = character.Humanoid
local maxHealth = humanoid.MaxHealth

local blur = Lighting.Blur
local blurStateNormal = {
	Size = 0
}

local blurState0 = {
	Size = 2
}

local blurState1 = {
	Size = 4
}

local blurState2 = {
	Size = 8
}

local tweenInfo = TweenInfo.new(5, 3, 0)

function onLowHealth(health)
	local healthPercentage = health / maxHealth
	
	if healthPercentage < 40 then
		local tween = TweenService:Create(blur, tweenInfo, blurState0)
		tween:Play()
		
	elseif healthPercentage < 20 then
		local tween = TweenService:Create(blur, tweenInfo, blurState1)
		tween:Play()
	
	elseif healthPercentage < 10 then
		local tween = TweenService:Create(blur, tweenInfo, blurState2)
		tween:Play()
		
	elseif healthPercentage > 40 then
		local tween = TweenService:Create(blur, tweenInfo, blurStateNormal)
		tween:Play()
	end
end

humanoid.HealthChanged:Connect(onLowHealth)

This way has many flaws as i can’t seem to turn it back whenever health is full again and it just looks unnatural, i’d appreciate if someone could point me into the right direction, i’m not really sure of any other way this could be done at all, this is what i tried to do with this script (but failed to do so, lol)

visual

Is it possible to do this without having any kind of “preset” and just tween naturally?

2 Likes

you just need some math:

if healthPercentage <= 40 then
    local tween = TweenService:Create(blur, tweenInfo, 80/healthPercentage)
    tween:Play()
elseif health > 40 then
     local tween = TweenService:Create(blur, tweenInfo, 0)
    tween:Play()
end
2 Likes

if i understood your problem correctly, you should be able to create a simple function like this:

local function SetProgress(progress, Blur)

    local Max = 56
    local oneOverProgress

    if progress == 0 then
        oneOverProgress = 0
    else
        oneOverProgress = 1/progress
    end

print(oneOverProgress * Max )

local TweenINFO  = TweenInfo.new(.1, Enum.EasingStyle.Linear , Enum.EasingDirection.In)       
local T1 = TweenService:Create( Blur, TweenINFO, {Size = oneOverProgress * Max } )
T1:Play()

end
1 Like

So your script would look like this:

local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local character = script.Parent
local healthPercentage = character.Humanoid.Health/Humanoid.MaxHealth

local blur = Lighting.Blur

local tweenInfo = TweenInfo.new(5, 3, 0)

humanoid.HealthChanged:Connect(function()
	if healthPercentage <= 40 then
        local tween = TweenService:Create(blur, tweenInfo, {Size = 80/healthPercentage)
        tween:Play()
    elseif health > 40 then
        local tween = TweenService:Create(blur, tweenInfo, {Size = 0)
        tween:Play()
    end
end

I changed some things to make it more efficient

1 Like

That won’t work though, as TweenService:Create() needs a dictionary as a third argument

It’s not a Gui, it’s Blur, however, i’ll see what i can do with the code you’ve posted as i think it’s a possible solution to the problem i’m having if i salvage it a little

I should be able to salvage this. This will work WAY better.

local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local character = game.Players.LocalPlayer.Character

local blur = Lighting.Blur
local humanoid = character:FindFirstChildOfClass("Humanoid") -- i do this for precautionary measures, people should do this more

local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

humanoid.HealthChanged:Connect(function(health)
	local healthPercentage = health/humanoid.MaxHealth -- references to properties don't save.
    local tween = TweenService:Create(blur, tweenInfo, {Size = healthPercentage)
    tween:Play()
end)
3 Likes

Thank you, i was just doing something like this right now, it’s hard to decide which one to mark as a Solution, as everyone who answered this helped me to understand the problem equally, @VegetationBush, @Jaycbee05 and you, i’ll mark yours as Solution but if anyone reads this, know that every answer was correct in a way.

2 Likes