Connection events breaking when player dies

I have this little local script that grabs any new ObjectValue added in ReplicatedStorage and then connects the Changed event to Text property of any gui imstances with the same name as that ObjectValue. This work great so far, except when the player dies it no longer updates the gui even though the ObjectValues are still updating.

And yes, I do have the ResetOnSpawn property disbaled on the target Gui instance.

here is the code:

local players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerGui = players.LocalPlayer.PlayerGui
local ValueObjectFolder = ReplicatedStorage.GuiObjectValues[players.LocalPlayer.UserId]

local function ConnnectValue(descendant)
    if descendant:IsA("Folder") then
        return
    else
        local key = descendant.Name
        local guiInstances = PlayerGui:GetDescendants()
        for i,v in pairs(guiInstances) do
            if v.Name == key then
                v.Text = descendant.Value -- we do this first so we dont have to wait for the ObjectValues to change
                descendant.Changed:Connect(function()
                    print("boop")
                    v.Text = descendant.Value
                end)
            end
        end
    end
end


ValueObjectFolder.DescendantAdded:Connect(function(descendant)
	ConnnectValue(descendant)
end)
1 Like

Could it be possible that you are running your provided script inside of PlayerScripts? If that is the case, it might just stop running the script altogether when you are resetting. (And loading in a new script that doesn’t have the connections, yet.) Moving the script into PlayerGui could potentially fix this.

1 Like

Could you please provide your object hierarchy, as without it this problem cannot be solved. (Take a screenshot of the game explorer fully expanded)

Once you do that, we can start helping to solve your problem. However, without the object hierarchy I can’t.

2 Likes

I don’t think it’s a case of your player dying, I think you’re hitting a bad case check here. If the code is in a non-resetting container, then it should not stop working once a character dies.

I’m specifically talking about the fact that you needlessly return out of the scope entirely. Don’t use empty or useless if statements, use it what you need.

if descendant:IsA("Folder") then
    return
else

Change this line to the following:

if descendant:IsA("ValueBase") then
    -- Other code
end

You do not need an else statement for this scenario.

Also, the main issue aside,

ValueObjectFolder.DescendantAdded:Connect(function(descendant)
	ConnnectValue(descendant)
end)

No need to do this.

ValueObjectFolder.DescendantAdded:Connect(ConnectValue)
1 Like

the script is in PlayerGui

not how how this would affect a disconnect, but maybe you see something I do not:

thanks!

I made the changes you recommend, yes my code was sloppy because I have tried many things to get it to work.

Your first suggestion of not returning a needless value was true, but of course didnt fix the problem in the OP.

Your second suggestion of changing this:

ValueObjectFolder.DescendantAdded:Connect(function(descendant)
	ConnnectValue(descendant)
end)

to this:

ValueObjectFolder.DescendantAdded:Connect(ConnectValue)

does not work because it does not pass the descendent into the function which is needed in that funtion.

I am also very unclear on how you think that will fix my problem? Quite literally, this code functions perfectly well as it was posted in the OP until the player dies.

here is the current code:

local players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerGui = players.LocalPlayer.PlayerGui
local ValueObjectFolder = ReplicatedStorage.GuiObjectValues[players.LocalPlayer.UserId]

local function ConnnectValue(descendant)
    if descendant:IsA("ValueBase") then
        print(descendant," is not a folder!")
        local key = descendant.Name
        local guiInstances = PlayerGui:GetDescendants()
        for i,v in pairs(guiInstances) do
            if v.Name == key then
                v.Text = descendant.Value -- we do this first so we dont have to wait for the ObjectValues to change
                descendant.Changed:Connect(function()
                    print("boop")
                    v.Text = descendant.Value
                end)
            end
        end
    end
end


ValueObjectFolder.DescendantAdded:Connect(function(descendant)
	ConnnectValue(descendant)
end)

Thanks to all who look a this, its a very strange problem indeed!

The issue is that your LocalScript is directly under PlayerGui. PlayerGui is a resetting container which means when the character respawns, that script will get cleared.

You will either need to migrate it to PlayerScripts and change some of your variable directories, or place it under the ResetOnSpawn false Gui itself.


It does. ConnectValue has a single parameter which accepts a descendant passed to it. DescendentAdded does as well. You can therefore remove the need for an anonymous function and call a single function instead.

Did you test this and tell me that it doesn’t work? If it doesn’t, that’s unintended behaviour. That is correct code.

This was before I saw your hierarchy. Lack of context leads me to make responses based on what I can see. However, now that you’ve provided a hierarchy, I know the issue clearly. My previous post, well, has no relevance anymore. Sorry for the confusion.

2 Likes

Yes, i have tried your line like this:

ValueObjectFolder.DescendantAdded:Connect(ConnectValue)

and it gives an error: Attempt to connect failed: Passed value is not a function

Otherwise, your solution was correct, for some reason moving the script in PlayerScripts instead of PlayerGui fixed this issue. I am not sure where that behavior of resetting all the scripts in that container is documented, or why it would do that.

Thanks!

If a object in PlayerGui is not under a ScreenGui with ResetOnSpawn set to false, the object under ScreenGui will be destroyed.

1 Like

Huh, that’s interesting. I’ll try a repro later to see what’s up, it should work. Though, in the end, it’s merely a suggestion and nothing really relevant.

At least your code works and you found a solution. :slightly_smiling_face:

1 Like

cc @colbert2677,

I figured out what the problem with the .DescendantAdded event connection was, your function is spelled ConnnectValue with three n’s, not two.
Therefore, the correct line would be:


ValueObjectFolder.DescendantAdded:Connect(ConnnectValue)

As colbert said, this is just a suggestion, and I only wrote this to clear things up.

2 Likes