Change Depth of Field When Hurt

I want to make it so that when a player gets hurt, things further away will be blurry but everything nearby stays in focus, and then fades away after a few seconds. I am trying to achieve this with a local script located inside “StarterPlayerScripts.” It simply doesn’t work, but I would like to see if you could change the code to make it work.

This is what I have so far:

Script
--Variables
local plr = game.Players.LocalPlayer
local char = game.Workspace:WaitForChild(plr.Name)
local cam = workspace.CurrentCamera

char.Humanoid.Changed:Connect(function(Change)
	if Change == "Health" then
		local hurt = Instance.new("DepthOfFieldEffect")
		local num = 0.75
		hurt.Parent = cam
		hurt.Name = "Suppression"
		hurt.FarIntensity = 0.75
		hurt.FocusDistance = 0.05
		hurt.InFocusRadius = 15
		local intensity = hurt.NearIntensity
		intensity = num
		for count = 1,15,1 do
			num = num - 0.05
			intensity = num
			wait(0.01)
		end
	end
end)

You need to reparent the hurt object to game.Lighting, not Camera.

Okay, but that didn’t really help as the depth of field blur doesn’t fade away.

Edit:
I have found the solution.

You were assigning to NearIntensity, which is the nearby blur, although I am pretty sure you want to fade the farther blur.

Therefore, you can use this:

--Variables
local plr = game.Players.LocalPlayer
local char = game.Workspace:WaitForChild(plr.Name)
local cam = workspace.CurrentCamera

char.Humanoid.Changed:Connect(function(Change)
if Change == "Health" then
	local hurt = Instance.new("DepthOfFieldEffect")
	hurt.Parent = cam
	hurt.Name = "Suppression"
	hurt.FarIntensity = 0.75
	hurt.FocusDistance = 0.05
	hurt.InFocusRadius = 15
	hurt.NearIntensity = 0.75
	
	for count = 0.75,0,-0.05 do
		hurt.FarIntensity = count
		wait(0.01)
	end
	hurt:Destroy()
end
end)
1 Like

Sorry, it’s not fading out as intended. The depth of field effect stays there and doesn’t change.

Ok thanks, the revised version now works like a charm!