GUI destroying on leave

Hey! I hope everyone is having a great day or night. I have been working on my datastore system and the save function relies on saving some of the values in GUIs. The only problem is that when the player leaves I get the error that the GUI is nil(non-existent). Is there any way to access the GUI before it gets destroyed or will I have to find an alternative? Thanks in advance.

If I understand your question correctly, I believe if you use the PlayerRemoving event you can do this…

I am using the player removing event. The problem is that the GUI destroys before I can access it. Here is the code

local save = function(plr)
	local slots = plr.PlayerGui.MainUi.PetMenu.Equipped.Slots:GetChildren()
    -- other save stuff
end

game.Players.PlayerRemoving:Connect(save)

and here is the error
image

I will say find an alternative. It’s better to have values in the player and have the GUI just read/control values there.

1 Like

I’ll go access a datastore thing of mine, and I’ll get back to you with the way to save the data of the gui @BoomBoomNewc

1 Like

Maybe use the .Changed event? And save it every time something in the GUI is changed?

1 Like

I think this may be best, though it does seem kind of inefficient given that I only need to check what slot the pet is equipped in.

You can just have a folder that mimmicks the slots with a StringValue or ObjectValue and then use that as a way to put it on the UI.

Here’s an example via picture;
image

You can personally name it however you’d like, but this is most likely the best way to do something like this avoiding any bad safety measures. The server can handle how the slots are ordered and the client can read it and put it on the interface.

1 Like

If you run ‘PlayerRemoving’ in a localscript (of the player removing), it still has PlayerGUI. You can run the function on the client, get the data you need, and send it to the server via remoteEvent.

1 Like

This is necessary, the other option will not work unless you are changing the pet slot from the server. Any changes made to UI by the client will remain only on the client, which means you must check the client GUI anyways, and the GUI in playerGui will just be the default UI coming straight from starterPlayerGui.

1 Like

Thanks I’ll try that and let you know of it doesnt work

1 Like