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
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.
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:
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.
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.
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.