Code doesn't work after player death

So I’m running this code:

REMOVED

It is supposed to enable a ScreenGui when a ClickDetector gets clicked, and then disable it if Humanoid.Running is fired.
Testing the game, it gave me these results:

When you spawn first, it works fine, but after the player dies, if you try again, the section of the script that disables the Gui is completely ignored, and the “b” doesn’t print.

What do I want?

  • I want to know why this happens and how I can fix it.
  • The Humanoid.Running will keep firing when the player walks, even after the GUI is disabled. I want to know if this affects performance and if so, I’d like to know how can I counter this so that it doesn’t lag the client.

All help is appreciated, thanks in advance.

1 Like

Do you have ResetOnSpawn Enabled?

1 Like

I just learned about that so my guess is no if by default it isn’t

it’s on by default. If it is currently on, turn it off.

1 Like

I added:

BackpackGui.ResetOnSpawn = false

Still gives the same result

1 Like

Okay, when the player dies, do you redefine the humanoid? and is this a character script, of a player script?

2 Likes

Since you’re open to pointing out bad practices, I’d like to mention that you should use Humanoid.Running:Once() instead of Humanoid.Running:Connect(), as otherwise you’re creating permanent connections every time you open the backpack.

3 Likes

Not sure what you mean by that, I just get the humanoid trough this:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:FindFirstChildOfClass("Humanoid")

This is a LocalScript inside the BackpackGui

1 Like

check to see if the humanoid is still there.

2 Likes

After dying, yes, it is still in my character’s Model

1 Like

You should try

player.CharacterAdded:Connect(function(character)
    Humanoid = character.Humanoid
end)

so that the Humanoid variable is always updated when your character resets.

3 Likes

This worked. However, do you guys have any tips on the second point of my post?

  • The Humanoid.Running will keep firing when the player walks, even after the GUI is disabled. I want to know if this affects performance and if so, I’d like to know how can I counter this so that it doesn’t lag the client.
local connection = humanoid.Running:connect(function() end)

connection:Disconnect()
2 Likes

Like I mentioned before, use Humanoid.Running:Once()

3 Likes

^^or that^^ (extra characters EVEN MORE CHARACTER!!!)

2 Likes

Using :Once or a :Disconnect() is essentially the same right?

1 Like

:Once is more efficient and better practice

2 Likes

Alright, I’ll try that.

Thanks all of you for your help!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.