PlayerGui is not a valid member of Player "0utputt"

When a player leaves the game, it should save the values inside the main gui in playergui, but it instead gives an error and cannot load/save it (i use datastore2 btw)

game.Players.PlayerRemoving:Connect(function(playerr)
	local playerData = ds2("data",playerr)
	playerData:Set({
		[1] = playerr.Values.Coins.Value,
		[2] = playerr.Values.AuraType.Value,
		[3] = playerr.PlayerGui.MainGui.Values.LastRolledAuraReturnable.Value,
		[4] = playerr.PlayerGui.MainGui.Values.LastRolledAura.Value,
		[5] = playerr.leaderstats.Spins.Value,
		[6] = {
			[1] = playerr.PlayerGui.MainGui.Values.Inventory.Slot1.Value,
			[2] = playerr.PlayerGui.MainGui.Values.Inventory.Slot2.Value,
			[3] = playerr.PlayerGui.MainGui.Values.Inventory.Slot3.Value,
			[4] = playerr.PlayerGui.MainGui.Values.Inventory.Slot4.Value,
			[5] = playerr.PlayerGui.MainGui.Values.Inventory.Slot5.Value,
			[6] = playerr.PlayerGui.MainGui.Values.Inventory.Slot6.Value,
			[7] = playerr.PlayerGui.MainGui.Values.Inventory.SlotsFull.Value
		},
		[7] = playerr.PlayerGui.MainGui.Values.QuickRollOn.Value
	}) 
end)
2 Likes

You cannot update the player GUI after they left the game. Just update it while they are in the game, by using the data from your datastore you update the gui, you don’t save the gui to the player and update it AFTER they have left the game.

3 Likes

This code really hurts to look at. Please use variables, it will make your life much easier.

game.Players.PlayerRemoving:Connect(function(playerr)
	local playerData = ds2("data",playerr)
	local values = playerr.PlayerGui.MainGui.Values
	playerData:Set({
		[1] = playerr.Values.Coins.Value,
		[2] = playerr.Values.AuraType.Value,
		[3] = values.LastRolledAuraReturnable.Value,
		[4] = values.LastRolledAura.Value,
		[5] = playerr.leaderstats.Spins.Value,
		[6] = {
			[1] = values.Inventory.Slot1.Value,
			[2] = values.Inventory.Slot2.Value,
			[3] = values.Inventory.Slot3.Value,
			[4] = values.Inventory.Slot4.Value,
			[5] = values.Inventory.Slot5.Value,
			[6] = values.Inventory.Slot6.Value,
			[7] = values.Inventory.SlotsFull.Value
		},
		[7] = values.QuickRollOn.Value
	}) 
end)

As for your issue, it appears that PlayerGui is not available for the Player because the Player is being removed from the game during PlayerRemoving. As far as I know, there is no way to prevent PlayerGui being active during player removal. As this thread quotes:

It might be a hassle to move everything, though.

Best of luck!

3 Likes