Odd Humanoid death behaviour

I have a function that clones every part and accessory of the player’s character for an afterimage effect.
This function uses the Player.Character as an argument.
For some reason, when a character dies, an instance of the dead body is still being remembered and cloned in my afterimage function, which can be seen below. Notice how my character on the brown brick is also apart of the afterimage effect.

https://gyazo.com/804882708d453557680571c2f9c84339.gif

Now I’ve reset a couple of times and you’ll see all the different locations I’ve reset at.
https://gyazo.com/440e63d2d37e84b41d46ec373f15bfad.gif

I’ve tried to use Player:LoadCharacter() when the Humanoid.Health > 1 but nothing seems to work.
It’s like my past character models are being remembered into my new one or something.

Any idea on how to fix this odd behaviour?¨

Edit:
Code and more context can be found in the replies by me.

Edit 2:

I went to a new place and the same behaviour persists.
My old dead characters are being cloned. You can literally see my dead characters parts on the ground:

https://gyazo.com/9663e409c6416beac1b82a2300a7a336.gif

The odd thing is these dead character parts also don’t exist anywhere in the workspace.

Here’s another example where I reset by the brown brick, and now you can see my old previous dead character being cloned and laying on the ground.

https://gyazo.com/bb6beb0d110dfae5475f82085f4ecd16.gif

Sadly I can’t help you too well without seeing some of the code but it looks like you aren’t deleting the previous clones.

We can’t help you until we see your code.

No, I do definitely delete the clones. The issue is not the clones themselves. The other “afterimages” you’re seeing are the remains of my past character models.

Edit:

What happens is not the clones not being deleted properly afterwards. The issue is that the character model that died, for some odd reason still gets remembered and cloned with the new character model.

So if I died 5 times in the game, it would clone 5 different character models, instead of just the one that’s sent as an argument.

I posted code in the replies with some more context

Would you mind properly indenting your code so it’s readable? It’s currently hard to tell if the code is erroring at the bottom before the call to Debris:AddItem happens.

Sorry but I’m not sure how you want me to indent it any differently than it already is. I can assure you though, it doesn’t error and the game:GetService("Debris"):AddItem(Model,1) works fine.

As demonstrated in this GIF, the model is destroyed after 1 second:

https://gyazo.com/55baf5c31f670253830047cca3761fe3.gif

Would you mind showing us more of the code? For example, the function that calls the “CreateAfterImage” function?

spawn(function()
		EffectModule:AfterImage(Character, 3, .15)
	end)

function EffectModule:AfterImage(Character, Iterations, Time)
	for i = 1,Iterations do
		CreateAfterImage(Character)
		wait(Time)
	end
end

The Character parameter is literally just Player.Character from a local script

Character.Humanoid.Died:Connect(function()
			print(Player.Character)
			print("Hey")
			Player.Character:Destroy()
			print(Player.Character)
			wait(2)
			Player:LoadCharacter()
		end)
	end)
end)

So I’m pretty sure this happens because the Roblox character is not being destroyed properly on Humanoid death. I tried to do this in a server script, and even after destroying the character, it still prints out Shurikanz, when I had expected it to print nil.

https://gyazo.com/65797a58a9ccbdb96402046de3681b31.png

I think this is expected behaviour. Destroy does not remove a value from a variable or property (It sets it’s parent to nil and disconnects all connections), for example:

local part = Instance.new(“Part”)
part:Destroy()
print(part)

You can set Player.Character = nil if you want to clear the Player.Character property.

4 Likes

Alright, so I tried to say:

game:GetService('Players').PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function()
			wait(1)
			Player.Character = nil
			Player:LoadCharacter()
		end)
	end)
	Player:LoadCharacter()
end)

And then I walked onto a black brick to highlight where I’m standing at. I reset ontop of the black brick and my character has its joints broken. I then respawn as a new character, and my local script now has a reference to this new character. When I run my function, my previous dead character, with all its joints broken, is still being cloned however ontop of the black brick:

https://gyazo.com/010bad4b3198f6c24569d2fe6441f91c.gif

I then reset myself 3 more times, which now makes it clone 4 of my previous old character models. The only solution I’ve found to get rid of the character models is to rejoin the game, but resetting/dying will still result in this odd behaviour:

https://gyazo.com/cec2e64bfa9550ee9ec02dc7f1c20b98

I don’t know if this has something to do with R15 specifically or same thing occurs with R6. I definitely do not have any reference to old character models, so I don’t understand why this happens. As seen in my code, I have tried to set Player.Character = nil prior to loading a new character, and the issue still persists.

Edit:

The cloning happens all client side. However, it’s only the local player that sees these dead clones of character models. If I FireServer and the server FireAllClients with the relevant Character as parameter, to render the same afterimages, they do not have this issue

I think you need to provide more code showing the circumstances under which EffectModule:AfterImage is called for us to be able to help with this issue.

One tactic I would recommend for debugging issues like this is try breaking it down into the simplest possible reproduction of the issue, just a script that calls CreateAfterImage in a while true do loop on an empty baseplate with none of the rest of the code in your game.

3 Likes

I deleted everything from my code that wasn’t necessary for the afterimage function to run at a completely new fresh place. As I expected it still happens:

https://gyazo.com/091f1ce4401a9a08c9add9628910ae79.gif

Are you confident that it’s an issue with my code, and not how Roblox handles characters upon humanoid death on the client? As mentioned earlier, this issue is only seen by the local player, and only happens upon humanoid death. Simply rejoining the game will get rid of any reference to previous character models. I do not store the old character model in anyway, I’m pretty sure. I’ve been testing for hours now, so I can’t help but think this is not an issue with my code, but how humanoid death works in the background for the client.

Edit:
I made the place uncopylocked for testing.
Everything can be found sindie the StarterGui → Client.

Client requires the environment module script.
The environment module script handles player input, in this case double tapping W.
The effectmodule handles the afterimage function.

https://gyazo.com/d45bf6cba0d0b386a17e610e2da75886.png

Link: https://www.roblox.com/games/4814117502/Shurikanzs-Place-Number-94

So after debugging at the new place, I found out that the module script remembers the old instance of the character model, and that information carries over once you require the module again, and again and so on. So what I did was add this in my module script:

Character.Humanoid.Died:Connect(function()
	Character = nil
end)

And now it works as intended :smiley:

At first I was setting the Character = nil server-side, but I failed to remember that this issue only onccured on the client, so of course I had to set it to nil on the client haha.

You could also do this in a character-bound script so that it automatically resets itself.