Magnitude breaks on death

I wrote this script which makes players fall down if they get to close to a part, it works fine, but whenever a player dies it glitches and doesn’t work. Can someone please help?

LocalScript

1 Like

Hi Yellow, when your character dies, the HumanoidRootPart becomes nil as it has been removed from the game, therefore the part doesn’t exist and your code will trying to be index a Position from a nil part.

To fix this, you’d most likely want to check if humanoidrootpart exists and most likely run the while loop in a CharacterAdded event, thank you.

2 Likes

Thank you so much for your help, I’ll try to get it to work based off of the knowledge you have provided me. :slightly_smiling_face:

@EdibleGames has identified the problem however a solution is still unclear.

Use the CharacterAdded event of the player, and put the loop inside of it.

1 Like

Like this?

https://gyazo.com/c9330d4f8dc3a1876f3413072e95bb44

This isn’t working at all now.

This problem has still not been fixed.

Let’s use the edit feature instead of creating multiple posts. Anyway, move the HumanoidRootPart variable inside the function and use the character parameter of the CharacterAdded event.

1 Like

https://gyazo.com/a45df6d86a25356efcf738800a293c3c

Yes, does it work? If not, are there any errors?

1 Like

The problem is the script is still using the old humanoid root part’s position instead of the new one, we need to somehow like reset the position or something on death.

Instead of doing while wait() do, use the conditional and do while HumanoidRootPart do and put the wait() statement inside of the loop. By the way, I would recommend using RunService.Stepped instead of wait().

1 Like

Thank you so much for your help, but I figured it out, instead of using a variable which stored the humanoid root part I directly referenced it which worked!

    if (Character.HumanoidRootPart.Position - TrollPart.Position).Magnitude < AgroDistance then
        TrollPart.Transparency = 1
        TrollPart.CanCollide = false
        wait(1)
        TrollPart.Transparency = 0
        TrollPart.CanCollide = true
1 Like

Again, thank you so much! I wouldn’t have been able to do it without all of your help!

1 Like