My script that should reset a GUI after you die seems to work only once

I’ve made a main menu GUI in my game, and I wanted to make it so that once you die, the menu will load back up. I made this script below to do so:


    p = script.Parent.Parent.Parent
    wait(5) -- wait for character to load

    p.Character.Humanoid.Died:Connect(function()
          wait(5)
          p.PlayerGui.MainMenu.Enabled = true
          workspace.Menu:Play() -- "Menu" being a sound object
         print("test")
    end)

I tested this out in game and it seemed to only run the script once after I died. After that, it stopped working. For context, this is a localscript located in a folder in StarterPlayerScripts. I also tried to use an alternate version of this script in ServerScriptService, but that seemed to work even less well than this one.

In case it helps, this is the script for the button that closes the menu:


script.Parent.MouseButton1Click:Connect(function()
    script.Parent.Parent.Parent.Enabled = false
    workspace.Menu:Stop()
end)

Apologies if the script formatting is wrong, this is my first time posting here.

1 Like

ScreenGuis have a property called ResetOnSpawn, so all you really should need is a script to make the gui invisible once the player is done using it. As long as it is placed in StarterGui it will automatically appear again. Could you go into more depth on why you need a script to do this feature?

3 Likes

As soon as you die, the event will be removed. Cause the Character will no longer exist. Instead you could write something like this

local Player = game:GetService("Players");
Player.CharacterAdded:Connect(function(Char)
   Char.Humanoid.Died:Connect(function()
       wait(5);
       Player.PlayerGui.MainMenu.Enabled = true;
       workspace.Menu:Play();
    end)
end)

Or you could enable ResetOnSpawn, if that would help you more instead of requiring a script.

1 Like

So I would just need to enable “ResetOnSpawn” and I can get rid of the first script mentioned above?

Yeah, essentially setting ResetOnSpawn to true refreshes the entire object when the Character Respawns

I figured out the problem - I originally had that screen disabled, because the first screen that comes up is a loading screen, which when its done, enables the main menu. I had tried to use ResetOnSpawn but got confused on why it wasn’t working. I made it so when the game starts, the main menu is enabled, but disabled again right as the loading screen pops up. Thanks for the help.

1 Like

No problem, and happy you could figure out the issue.