Returning player FOV to default after player's death

hello yet again
i have a script that, given an input of the Q key, increases FOV via a for loop that adds + 1 each 0.025 second.
said player can die, and in such scenario the player respawns with FOV that differs from default.
image
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
player’s FOV
any idea if i could prevent that? i tried adding an if statement that then breaks the loop, but it brought similar results. here’s the script.

local inputservice = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local replicatedstorage = game:GetService("ReplicatedStorage")
local active = false
local origwalkspeed = nil
local camera = game:GetService("Workspace").CurrentCamera
local sounds = game:GetService("Workspace").Sounds

local speedupamount = 8
local abilityduration = 2.5
local addFOVamount = 30

inputservice.InputBegan:Connect(function(input, isTyping)
	if not active then
		origwalkspeed = character.Humanoid.WalkSpeed
	end
	if active then
		return
	end
	if isTyping then
		return
	end
	if input.KeyCode == Enum.KeyCode.Q then
		active = true
		character.Humanoid.WalkSpeed = origwalkspeed + speedupamount
		sounds["pumpkin_drop.wav"].Playing = true
		for i = 1, addFOVamount do
			camera.FieldOfView += 1
			task.wait(0.025)
		end
		task.wait(abilityduration)
		for i = 1, addFOVamount do
			camera.FieldOfView -= 1
			task.wait(0.025)
		end
		character.Humanoid.WalkSpeed = origwalkspeed
		active = false
	end
end)

^^^^^^
(local script)
also, any feedback on the script is welcome.

2 Likes

In a server script, you can just do

game.players.PlayerAdded:Connect(function(player)
   plr.CharacterAdded:Connect(function(char)

   end)
end)

I dont know how to change the players FOV :sweat_smile:
but you would do it inside there.
There may not be brackets after the ends, but i forgot whether or not there were.

If you can only change it in a local script, use a remote event, and fire it to the player.

1 Like

Store the default FOV when the LocalPlayer dies; set the FOV back to default

3 Likes

Assuming this is a LocalScript in StarterCharacterScripts,

-- your other declarations
local origwalkspeed = character.Humanoid.WalkSpeed
local origfov = camera.FieldOfView
local tweenservice = game:GetService("TweenService")

local speedupamount = 8
local abilityduration = 2.5
local addFOVamount = 30

local to = tweenservice:Create(camera, TweenInfo.new(1), {FieldOfView = camera.FieldOfView + addFOVamount})
local from = tweenservice:Create(camera, TweenInfo.new(1), {FieldOfView = origfov})

inputservice.InputBegan:Connect(function(input, isTyping)
	if active or isTyping then
		return
	end

	if input.KeyCode == Enum.KeyCode.Q then
		active = true

		character.Humanoid.WalkSpeed += speedupamount
		sounds["pumpkin_drop.wav"]:Play()
		to:Play()

		if to.Completed:Wait() == Enum.PlaybackState.Completed then
			task.wait(abilityduration)

			character.Humanoid.WalkSpeed = origwalkspeed
			from:Play()
		end

		active = false
	end
end)

character.Humanoid.Died:Connect(function()
	to:Cancel()
	camera.FieldOfView = origfov
end)

As for script feedback, see the Roblox style guide.

I don’t think you need to store it, you can just make a variable of default fov, then when their character joins, set their fov as the default.

I went through your script and tightened it up to do as you wish, but I never hit that error in the first place with your first script. All I can think is somehow the player was respawning so fast the loop to reset the camera wasn’t finished. I tested in StarterCharacterScripts and StarterPlayerScripts.

local inputservice = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local camera = game:GetService("Workspace").CurrentCamera
local sounds = game:GetService("Workspace").Sounds

local defaultFOV = camera.FieldOfView
local speedupamount = 8
local abilityduration = 2.5
local addFOVamount = 30
local active = false
local origwalkspeed

local function setup(char)
	origwalkspeed = char:WaitForChild("Humanoid").WalkSpeed
	camera.FieldOfView = defaultFOV
end

setup(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(setup)

inputservice.InputBegan:Connect(function(input, isTyping)
	if isTyping or active or not player.Character then return end
	if input.KeyCode == Enum.KeyCode.Q then active = true
		local hum = player.Character:FindFirstChild("Humanoid")
		if hum then	hum.WalkSpeed = origwalkspeed + speedupamount
		end	sounds["pumpkin_drop.wav"].Playing = true

		for i = 1, addFOVamount do
			if not player.Character or not player.Character.Parent then break
			end	camera.FieldOfView += 1 task.wait(0.025)
		end	task.wait(abilityduration)

		for i = 1, addFOVamount do
			if not player.Character or not player.Character.Parent then break
			end	camera.FieldOfView -= 1 task.wait(0.025)
		end

		if hum then	hum.WalkSpeed = origwalkspeed
		end	camera.FieldOfView = defaultFOV
		active = false
	end
end)

nifty, thanks! i’ll look into how tweenservice works.

1 Like

Another bit:

You might want to insert

from.Completed:Wait()

after

inputservice.InputBegan
	if input.KeyCode
		if to.Completed:Wait()

			from:Play()
			--from.Completed:Wait()

if you want the Q key to wait until the FOV has returned to the default before the user is allower to animate it again.

If there’s a cooldown, you can also just wait that time there instead.

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