Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to intensify effect then remove on death?

This is a local script that activates the blur effect once the client reaches a certain amount of health. From then on the I planned on increasing the intensity or "Size" of the blur as the health lowers and once the player dies I used the Died event to remove the effect. Two problems: As the health decreases the intensity only decreases instead of increasing and the second problem is that each time the character dies, the previous instance stays and another is put in place.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local effect = Instance.new("BlurEffect", workspace.CurrentCamera)
effect.Enabled = false

local Humanoid = char:WaitForChild("Humanoid")
Humanoid.Changed:connect(function()
	if Humanoid.Health <= 75 then
		effect.Enabled = true
		effect.Size = 20 + (Humanoid.Health)
	end
end)


game:GetService('Players').PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			workspace.CurrentCamera:ClearAllChildren()
		end)
	end)
end)

2 answers

✓ Accepted answer

As we need the local player model this scrip should be placed in startercharacterscripts so we don't need to check for the character.

local plr = game.Players.LocalPlayer -- we dont need to check for the player as this script is in startercharacterscripts
local Humanoid = script.Parent:FindFirstChild("Humanoid") -- find the Humanoid in the player
local curHP = Humanoid.Health --to check the hp 

local effect = Instance.new("BlurEffect", workspace.CurrentCamera)
effect.Enabled = false

Humanoid.HealthChanged:connect(function(hp)
    if Humanoid.Health <= 75 and effect then -- also include that we must have an effect
        effect.Enabled = true --i dont know why you need this?
        effect.Size = math.floor((curHP - hp) - 25) -- work out the hp change then take 25 to reach 100 hp
    end
end)

Humanoid.Died:connect(function()
	if effect then effect:Destroy() end -- remove the effect when the humanoid dies
end)

Hope this helps, comment if you need any more info or explanation of why this script works.

2 comments

0

Thanks for the input, just saw this.

0

Alright it works properly and the effect is removed on death. Thanks again.

For your first problem your math was WAY off. you were saying add the health to the size which will make size bigger as you heal. This makes it so it takes the absolute value of health - 80 and uses the absolute value of 20 - that. Adjust the equation as you want. Now for your next you added a lot of unnecessary functions, you just need to wait for char to die, thats all.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local effect = Instance.new("BlurEffect", workspace.CurrentCamera)
local Humanoid = char:WaitForChild("Humanoid")

effect.Enabled = false

Humanoid.Changed:connect(function()
	if Humanoid.Health ~= 0 then
    	if Humanoid.Health <= 75 then
       	 	effect.Enabled = true
       		effect.Size = math.abs(20 - math.abs(((Humanoid.Health)-80)))
		end
    end
end)

char.Humanoid.Died:connect(function()
	workspace.CurrentCamera:FindFirstChild("Blur").Enabled = false
end)

2 comments

0

Thanks for the input, also just saw this

0

The Died event won't fire for some odd reason, I don't know if this is a studio issue but I haven't tested it in an actual game yet.

This discussion is preserved as it was. New answers, comments, and votes are closed.