Changing Fov if damaged

How would I go about doing a script that changes fov if the player is damaged and returns to normal?

1 Like

In a local script just change the Workspace.CurrentCamera.FieldOfView property when the player gets damaged and then change it back over a fixed time period to the original (or tween it back for a smooth transition)
https://developer.roblox.com/en-us/api-reference/property/Camera/FieldOfView

How would I go about checking if a player is damaged? When I tried to script it changed even when player was healing

Put this in StarterCharacterScripts:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Human = Character.Humanoid
local CurrentHealth = Human.Health
local Cam = workspace.CurrentCamera
local FOV = Cam.FieldOfView

Human.HealthChanged:Connect(function(Health)
	if Health < CurrentHealth then
		if Cam.FieldOfView == FOV then
			Cam.FieldOfView = FOV + 20
			spawn(function()
				wait(.25)
				Cam.FieldOfView = FOV
			end)
		end
	end
	CurrentHealth = Health
end)

You should save the old health in a variable and check if the new health is less than or greater than the old health to change the FOV accordingly. After this, you set the oldHealth variable to the current health!

Is there a possibility to tween it?

Yeah, here’s an updated script with tweening:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Human = Character.Humanoid
local CurrentHealth = Human.Health
local Cam = workspace.CurrentCamera
local FOV = Cam.FieldOfView
local TS = game:GetService("TweenService")

local Info = TweenInfo.new(
	.125,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	true,
	0
)

local Goal = {}
Goal.FieldOfView = FOV + 20

local Tween = TS:Create(Cam, Info, Goal)

Human.HealthChanged:Connect(function(Health)
	if Health < CurrentHealth then
		if Cam.FieldOfView == FOV then
			Tween:Play()
		end
	end
	CurrentHealth = Health
end)

You can change the settings to fit your needs.

Thank you so much I was struggling a lot, and I have learned so much

1 Like