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)
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.
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.
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.
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.
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.
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: