Script Breaks Upon Death

im using a configrated version of a running system. its simple system for running and works all fine!
but problem is if characters dies once. when player tries to run again animation plays but speed and fov doesnt change at all, how can i fix it?
script:


-- Settings
local sprintSpeed = 27
local defaultSpeed = 12.5

local sprintFov = 80
local normalFov = 70

local sprintKey = Enum.KeyCode.LeftShift
local sprintButton = Enum.KeyCode.ButtonL3

--// Services

-- External services
local userInputService = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")

-- Internal services
local replicatedStorage = game:GetService("ReplicatedStorage")
local playersService = game:GetService("Players")

-- // Variables

-- Comms variables
local sprintEvent = replicatedStorage:WaitForChild("SprintEvent")

-- Player variables
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local camera = workspace.CurrentCamera

-- Animation variables
local sprintAnim = script:WaitForChild("SprintAnim")
local sprintTrack = humanoid:LoadAnimation(sprintAnim)

-- Booleans
local sprinting = false

-- Functions
local function tweenFov(duration, fov)

	local tween = tweenService:Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Quad), {FieldOfView = fov})
	tween:Play()

	spawn(function()
		tween.Completed:Wait()
		tween:Destroy()
	end)

end

-- // Events

-- Input handling
userInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == sprintKey and not processed then
		if not sprinting then
			sprinting = true

			if humanoid.MoveDirection.Magnitude > 0 then
				if not sprintTrack.IsPlaying then
					sprintTrack:Play(0.25)
				else if humanoid.MoveDirection.Magnitude <= 0 then
						sprintTrack:Play(0.25)
						wait(0.3)
						sprintTrack:Stop()
					end
				end
			end
			if sprintTrack.IsPlaying then
				sprintEvent:FireServer(sprintSpeed)
				tweenFov(0.35, sprintFov)
			end

		end
		repeat 
			task.wait()
			if humanoid.MoveDirection.Magnitude <= 0 then
				sprintTrack:Stop()
				sprinting = false
				sprintEvent:FireServer(defaultSpeed)

				tweenFov(0.35, normalFov)
			end
		until false
	elseif input.KeyCode == sprintButton and not processed then
		if sprinting then
			sprinting = false

			if sprintTrack.IsPlaying then
				sprintTrack:Stop()
			end

			sprintEvent:FireServer(defaultSpeed)

			tweenFov(0.35, normalFov)
		else 
			sprinting = true

			if humanoid.MoveDirection.Magnitude > 0 then
				if not sprintTrack.IsPlaying then
					sprintTrack:Play(0.25)
				else if humanoid.MoveDirection.Magnitude <= 0 then
						sprintTrack:Play(0.25)
						wait(0.3)
						sprintTrack:Stop()
					end
				end
			end
			if sprintTrack.IsPlaying then
				sprintEvent:FireServer(sprintSpeed)

				tweenFov(0.35, sprintFov)
			end

		end

	end
	repeat 
		task.wait()
		if humanoid.MoveDirection.Magnitude <= 0 then
			sprintTrack:Stop()
			sprinting = false
			sprintEvent:FireServer(defaultSpeed)

			tweenFov(0.35, normalFov)
		end
	until false
end)

userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == sprintKey then
		sprinting = false

		if sprintTrack.IsPlaying then
			sprintTrack:Stop()
		end

		sprintEvent:FireServer(defaultSpeed)

		tweenFov(0.35, normalFov)
	end
end)

-- Animation handling
humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if humanoid.FloorMaterial == Enum.Material.Air then
		if sprinting then
			sprinting = false
			repeat wait()
				if sprintTrack.IsPlaying then
					sprintTrack:Stop()
				end
			until humanoid.FloorMaterial ~= Enum.Material.Air

			if sprinting then
				if humanoid.MoveDirection.Magnitude > 0 then
					if not sprintTrack.IsPlaying then
						sprintTrack:Play(0.25)
					else if humanoid.MoveDirection.Magnitude < 2 then
							sprintTrack:Play(0.25)
							wait(0.3)
							sprintTrack:Stop()
							sprinting = false
						end
					end
				end
			else 
				sprinting = false

				if sprintTrack.IsPlaying then
					sprintTrack:Stop()
				end

				sprintEvent:FireServer(defaultSpeed)

				tweenFov(0.35, normalFov)
			end
		end
	else 
		if sprinting then
			if humanoid.MoveDirection.Magnitude > 0 then
				if not sprintTrack.IsPlaying then
					sprintTrack:Play(0.25)
				else if humanoid.MoveDirection.Magnitude < 2 then
						sprintTrack:Play(0.25)
						wait(0.3)
						sprintTrack:Stop()
						sprinting = false
					end
				end
			end
		end
	end
end)

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		if sprinting then
			if not sprintTrack.IsPlaying then
				sprintTrack:Play(0.25)
			else if humanoid.MoveDirection.Magnitude <= 0 then
					if sprinting then
						sprintTrack:Play(0.25)
						wait(0.3)
						sprintTrack:Stop()
					end
				end
			end
		end
	end
end)

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection.Magnitude <= 0 then
		sprinting = false
		sprintEvent:FireServer(defaultSpeed)

		tweenFov(0.35, normalFov)
	end
end)
while true do
	task.wait()
	if sprinting then
		if not sprintTrack.IsPlaying then
			sprintTrack:Play(0.25)
		else 
			local Pos = character.HumanoidRootPart.Position
			--print(Pos)
			wait(0.3)
			local Pos2 = character.HumanoidRootPart.Position
			--print(Pos2)
			if Pos == Pos2 then
				sprintTrack:Stop()
				sprinting = false
				sprintEvent:FireServer(defaultSpeed)

				tweenFov(0.35, normalFov)
			end
		end
	end
end

Script of WalkSpeedHandler at serverscriptservice


-- // Services

-- Internal services
local replicatedStorage = game:GetService("ReplicatedStorage")
local playersService = game:GetService("Players")

-- // Variables

-- Comms variables
local sprintEvent = replicatedStorage:WaitForChild("SprintEvent")

-- // Events

-- Comms events
sprintEvent.OnServerEvent:Connect(function(player, walkSpeed)
	
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")

	humanoid.WalkSpeed = walkSpeed
	
end)

What is the location of the LocalScript?

inside StarterCharacterScripts

I don’t see the point in the server script? Changes to the WalkSpeed of the humanoid from the client replicate automatically. Try removing the event functionality and making it all local.

That ^^^

Also OP, are there any errors in the console ?

theres no any console errors, ima try what “Xitral” said now

Also,

-- Player variables
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

If the script is inside the character, you don’t need to do this you can just do

local character = script.Parent
local humanoid = character:WaitForChild(“Humanoid”)

it worked for speed but now for some reason tween of camera is stopped working

It is bad code etiquette to use script.Parent when refering to services or client objects; it can be unclear and doing CharacterAdded:Wait() saves you from potential client load issues.

Was the camera tween originally working prior to death?

But the script is inside the Character and always inside the character. CharacterAdded:Wait() is never needed because of that

changed it as u said but, it didnt fixed the camera

yea it was working before death

@InedibleGames do u got any idea?

try adding a print statement inside of the FOV function and check to see if the function even is firing as it should

can u show the hierarchy of both scripts in the explorer

local playersService = game:GetService("Players")
local player = playersService.LocalPlayer
local character = player.Character or player:GetPropertyChangedSignal("Character"):Wait()

i found something interested here it is

Function looks like that

local function tweenFov(duration, fov)
     print("Fired Fov")
	local tween = tweenService:Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Quad), {FieldOfView = fov})
	tween:Play()

	spawn(function()
		print("Fired Fov Spawn")
		tween.Completed:Wait()
		tween:Destroy()
	end)

end

and here is the output
Before Death
https://gyazo.com/c3a3dc05635acb4bd04e0501d6a43133
(The time it stops printing is when i start to run)

After Death
https://gyazo.com/acdceca51c9df7da5714542b307e2a0f
( it doesnt slow down when i run)

Also seeing as you are just deleting the tween after you should write it as:

tweenService:Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Quad), {FieldOfView = fov}):Play()

Roblox will handle the dump collection for you, instead of having to spawn a function that deletes it.

This also may solve the problem as you are inducting fewer spawns

nope didnt fix do u got any other idea?