Modulescript that handles death effects stops running when player respawns

hi, I am having issues with a modulescript.

in my game players have a model or “fake” body that is cloned by a modulescript on player death. the clone is in the workspace and completely seperate from the player. the module creates special effects then removes the clone.

this is the server script that watches for the player’s death(a test module was used in place of the original for demonstration purposes):

local plr = script.Parent.Parent.Parent
local char = game.Workspace:WaitForChild(plr.Name)

char.Humanoid.Died:Connect(function()
local mod = require(game.ReplicatedStorage.Mods.General.OnDeathTest)
mod.death(char)
end)

a pretty simple script, now here is the modulescript:

local module = {}


module.death = function(hummodel)

print("starting module")


--this would normally be a "wait()"--
for i = 1, 100 do
wait(0.1)
print("waiting...")
end
------------------------------------




------------------------
--SPECIAL EFFECTS CODE--
------------------------





print("finished loop")
end


return module

expected result: the module should continue to run and apply effects and then clean up.
acual result: the module runs but then stops abruptly as soon as the player respawns.

the purpose of the test module is to show that the for loop stops when the player respawns.
this is strange as the module was called by the server so there shouldn’t be any interference from the player. what can I do to fix this?

Likely because you gave the module the original character, but when a player dies and respawns, the original character is removed and replaced with a new one, rendering the old one useless

the player referenced in the module is only used to clone a fake body, wouldn’t the players model just be nil instead of shutting off the whole module?

If it tries to clone the fake body but finds that the character is “nil” then it will probably return an error, thereby stopping the script run as a whole

I thought this might have been another script so I ignored it:
image
after running the original module this shows up shortly after the player respawns.

StarterCharacterScripts would be a good place to store a script like this.
The character would be script.Parent if parented there as well.
And, as the name suggest, it runs everytime the player respawns, and returns the new character model.

sent the script to SCS

local char = script.Parent

char.Humanoid.Died:Connect(function()
local mod = require(game.ReplicatedStorage.Mods.General.OnDeath)
mod.death(char)
end)

still the same problem unfortunately.

You don’t need to require the module-script everytime by the way.
And, any errors, and does the code run?
Try adding a print after the event.

it was the same result as before the changes
image

I also tried moving the “require” out of the died event to see if that did anything, but it didn’t help.

if you test this yourself are you able to duplicate the results Im having?

Does it print after you reset your character?

yea it does. the last image was taken after ending test mode but it did come up during testing.

If it prints, what’s the issue now?

oh sorry I thought u meant with the infinite yield, no aside from that it prints nothing.

Works fine for me, make sure you saved the game before testing.

for this code:

local module = {}


module.death = function(hummodel)

print("starting module")
local v = hummodel

--this would normally be a "wait()"--
for i = 1, 100 do
wait(0.1)
print("waiting...")
end
------------------------------------




------------------------
--SPECIAL EFFECTS CODE--
------------------------





print("finished loop")
end


return module

this is my result:

image

to make sure there was no interference from other scripts I opened a blank template and ran the game.
the script never prints finished loop. when the player respawned it stopped at x61. ignore the disconnect I took the picture after stopping test mode.

This is the opposite problem to this problem:

How to prevent death of the model the player.Character changed TO? - Help and Feedback / Scripting Support - DevForum | Roblox

The model dies because ownership of the humanoid changed. The only way to enter a body without killing it is to remove the humanoid. I suspect the only way to leave a body (respawn) without killing it is to remove the humanoid.

I’ll be honest, I have not tried it.

it seems that respawning or re-parenting the script that required the module suddenly stops it.
currently I reparented the script to a place that does not reset upon respawning. this is a solution to the problem for the player but it does not help the npcs I have which use the same death module.
for that I will just have to hide the original body until the module finishes.

Oh, you don’t actually need the body? Then maybe you should use a bindable event for everything that happens now the model leaves.

To keep the body around, I hold HP > 0.