LocalScript not working after Player death

Hey All,

I’m having an issue where a LocalScript stops working after a player dies. Can you tell me what I need to modify so the LocalScript continues working after the Player respawns?

Here is the basic scenario:

  • Simple baseplate world with a spawn point. The baseplate made smaller for testing so I can quickly fall off and die.
  • I’ve added a TextButton within a Frame on the screen so that when clicked another Frame with a TextLabel will pop up. Here is a pic:

I’ve added a LocalScript to the StarterPlayerScripts Folder. The LocalScript handles detecting the TextButton being clicked to make the Frame with the TextLabel visible or invisible. Here is my LocalScript:

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local screenGui = playerGui:WaitForChild("ScreenGui")
local testButton = screenGui.TestButtonFrame.TextButton
local popUpFrame = player.PlayerGui.ScreenGui.PopUpFrame

local function popUp()
    if popUpFrame.Visible == true then
        popUpFrame.Visible = false
    elseif popUpFrame.Visible == false then
        popUpFrame.Visible = true
    end
end

testButton.Activated:Connect(popUp)

I understand that there is a difference between a Player and a Character. It is a Character that dies. That scripts that make use of a Character may need additional code to reset the Character information after a Character dies. This LocalScript doesn’t make use of the Character, so I figure the coding is good.

I purposefully placed the LocalScript in the StarterPlayerScripts Folder because I figured that folder is maybe more stable that the StarterCharacterScripts. I dunno. Maybe there’s no difference.

Here’s the issue…
When I the game starts, I can click the Test Button and make the PopUp Frame visible/invisible just fine. However, if I walk off the baseplate, die, then respawn, clicking the TextButton no longer works to make the PopUp Frame visible.

Here’s a video of me testing it out, showing the button working then not working after death.

robloxapp-20210624-1250539.wmv (630.5 KB)

Any suggestions on what I need to change?

Set the property “ResetOnSpawn” to true on the ScreenGui, and see if that fixes your problem.

1 Like

tick ResetOnSpawn to true under the screengui

1 Like

The “ResetOnSpawn” box is checked. So that’s not the issue.

set it to false then see if that works

Yes, it works now. That’s very weird.

Maybe your variable for your pop up box was only defined once, try making it so the variable is defined every time you click the button, example:

MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local screenGui = playerGui:WaitForChild("ScreenGui")
local testButton = screenGui.TestButtonFrame.TextButton
local popUpFrame = player.PlayerGui.ScreenGui.PopUpFrame

Ooo… that’s an interesting approach. I’ll give it a try.

Nope, at least the way I approached it didn’t work (trying to do what you suggested).

Here’s what I did…

  • Rechecked the “ResetOnSpawn” box

  • Modified the script as such. I had to keep the initial local variables in order for the LocalScript to know what the TextButton was.

    local player = game.Players.LocalPlayer
    local playerGui = player:WaitForChild(“PlayerGui”)
    local screenGui = playerGui:WaitForChild(“ScreenGui”)
    local testButton = screenGui.TestButtonFrame.TextButton
    local popUpFrame = player.PlayerGui.ScreenGui.PopUpFrame

    local function popUp()
    player = game.Players.LocalPlayer
    playerGui = player:WaitForChild(“PlayerGui”)
    screenGui = playerGui:WaitForChild(“ScreenGui”)
    testButton = screenGui.TestButtonFrame.TextButton
    popUpFrame = player.PlayerGui.ScreenGui.PopUpFrame

      if popUpFrame.Visible == true then
        popUpFrame.Visible = false
      elseif popUpFrame.Visible == false then
        popUpFrame.Visible = true
      end
    

    end

    testButton.Activated:Connect(popUp)

So here are solutions that have worked.

  1. Unchecking the ResetOnSpawn box under the ScreenGui property worked (shout out to Acratixx); though I don’t know why. Plus you may want a player’s GUI to reset after death, so this option defeats the purpose of the ResetOnSpawn property.
  2. Moving the LocalScript from the StartingPlayerScripts Folder to the StartingCharacterScripts Folder allows the LocalScript to continue functioning after death. This seems easy enough and is what I will do in my main game. But it’s not clear why a LocalScript behaves different depending on which of these two Folders it’s placed into.
2 Likes