Hey devforum how to make module script run every time player dies/respawns?

Hey, Devforum.

I have a quick question I script games in module hierarchy/oop programming style. I made a mechanic for the player to lock on to enemies or other players as a combat mechanic. The problem is when the player dies and respawns the module doesn’t run a second time, meaning if I had a variable like Character = player.Character. This will no longer work because the player.Character died, and is now replaced with a new character so how can I ‘re-require’ them or find a way to update their variables and functions when the player respawns?

My scripts are set up like this;

You could use

		local PLR = workspace:WaitForChild(player.Name)
			PLR = workspace:WaitForChild(player.Name)

The first function fires when a player joins the server
The second one makes sure the character is loaded and fires
The third one fires once the character is dead and then replaces the old PLR to the new PLR once respawned.

I’m sure there is a easier way to do this but this works for me

This works great I had this solution in my mind the I just hoped their would be a cleaner solution. I do have on problem in my DynamicHeadBobble script I use a spring. And Im getting a lot of problems from it when trying this method it wont seem to update the spring variable.

here’s my code for requiring the spring;

local EGO_MOOSE_SPRING = require(game.ReplicatedStorage.ModuleInitializer.Functions.EgoMooseSpring)
local egoMooseSpring = EGO_MOOSE_SPRING.new(0,0,0)
egoMooseSpring.angularFrequency = 4.69

local function DynamicCamera(deltaTime)
	workspace.CurrentCamera.CFrame *= CFrame.Angles(0, 0, math.rad(math.sin(tick()*50)*egoMooseSpring.p)*0.2)

	egoMooseSpring.target = speed/16

this works fine until I die then it stops updating the spring and all that here’s my variable update code;

local function update()
	player = game.Players.LocalPlayer
	character = player.Character
	egoMooseSpring = EGO_MOOSE_SPRING.new(0,0,0)
	egoMooseSpring.angularFrequency = 4.69


The solution I came to is to just update module variables inside a renderstepped or the function that needs to use them I feel as if this will lag down the game but I’m not sure this is the only way I found after testing and researching online.